【问题标题】:Testing an array works with toEqual but not with toBe测试数组适用于 toEqual 但不适用于 toBe
【发布时间】:2019-12-30 20:11:16
【问题描述】:

我正在测试一个数组是否是我期望它使用的 toBe()。它通过toEqual(),但这不是一个好的检查 作为toBe(),因为我不希望返回的内容有任何错误的余地。

我已经尝试过使用toEqual(); 的解决方法。然而,虽然它本身可以工作,但我希望 toBe 也能通过,因为我不想犯错。

getAssignedChildren = () => {
    const { parent, allChildren } = this.state;
    const { children: assignedChildren } = parent || {};
    const assignedChildNames = [];

    if (!assignedChildren) {
        return [];
    }

    assignedChildren.forEach((assignedChildId) => {

        const childData = allChildren.find(
            (child) => child.childId === assignedChildId,
        );
        assignedChildNames.push(childData.childName);
    });

    return assignedChildNames;
};    





it("should get the assigned children for a given parent, push the 
names of said children into an array, and display the names of the 
children on the corresponding parent\b's detail page", () => {
    const { children } = parent;
    const { records } = allChildren;
    const props = {
        someStore,
    };

// allChildren is an imported mock data object
    const wrapper = createWrapper(ParentDetails, props, true);
    wrapper.setState({
        parent: { children },
        allChildren: records,
    });
    wrapper.getAssignedChildren();

    // TODO: Refactor toEqual to toBe in order to apply stricter 
equality checking
// for reference, the mock data object causes there to be a matched 
child-to-parent array to have a length of 5
// I need this toEqual to be a toBe
    expect(wrapper.getAssignedChildren()).toEqual([
        records[0].childName,
        records[1].childName,
        records[2].childName,
        records[3].childName,
        records[4].childName,
    ]);

    expect(children.length).toEqual(wrapper.getAssignedChildren().length);

测试通过 toEqual。然而,toBe 说:

预期 ['child1', 'child2', 'child3', 'child4', 'child5'] 但得到 ['child1', 'child2', 'child3', 'child4', 'child5']。控制台错误消息显示:比较值没有视觉差异。请注意,您正在使用Object.is 与更严格的toBe 匹配器测试相等性。仅对于深度相等,请改用toEqual

【问题讨论】:

  • 正如它所说的toBe 正在使用Object.is,只有当数组引用相等时才会返回true。 toEqual 是这里要用到的
  • toBe===基本相同。一旦你在你的方法中创建了一个新数组(顺便说一句,你应该使用map 而不是forEach),toBe 不能通过。

标签: reactjs jestjs


【解决方案1】:

Jest 文档说明如下:

toBe 使用 Object.is 来测试完全相等。如果你想检查 对象的值,使用 toEqual 代替。

Object.is 给出两个值是否相同。如果是数组,它们是否具有相同的引用。 这就是为什么你需要使用toEqualrecursively checks every field of an object or array.

文档链接:https://jestjs.io/docs/en/using-matchers

Object.is:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

【讨论】:

  • @m-greco 如果回答有帮助,请考虑接受并点赞。
【解决方案2】:

Object.is 检查是否相等。请参见下面的代码。它不会比较数组的值。 我希望这能澄清你对 toBe 和 Object.is 的疑惑

//it will be false as it will check for referential equality
console.log(Object.is([1,2,3], [1,2,3]))
var arr = [1,2,3];
//it will be true
console.log(Object.is(arr,arr))
//but below will be false
console.log(Object.is(arr,[1,2,3]))
//same goes for objects
var obj = {value:1};
//it will be true
console.log(Object.is(obj, obj));
//but below will be false
console.log(Object.is(obj, {value:1}));

【讨论】:

    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2018-09-06
    • 2021-10-21
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多