【发布时间】:2019-01-18 16:18:12
【问题描述】:
我正在使用expect(item).toEqual(otherItem)。根据我读过的所有内容,expect({ a: "a", b: undefined }).toEqual({ a: "a" }) 应该可以通过。否则我应该使用toStrictEqual。 E.g. here, in "basic matches".
但是,我的输出失败了:
Array [
Object {
+ "alertId": undefined,
"attributes": Array [],
"destination": "_b",
"kind": "kind",
"linkable": "Unlinkable",
"source": "_a",
+ "validFrom": undefined,
+ "validTo": undefined,
},
- Object {
- "attributes": Array [],
- "destination": "_c",
- "kind": "kind",
- "linkable": "Unlinkable",
- "source": "_a",
- },
]
知道为什么吗?
【问题讨论】:
-
如果预期的对象没有与对象相同的属性,即使值未定义,测试也会失败。解决方案是修复您的函数以删除未定义的值或期望具有未定义值的属性在对象中。
-
expect([{ a: "a", b: undefined }]).toEqual([{ a: "a" }]);怎么会通过呢?我还在大约 10 次测试中测试了这个函数,尽管它们都具有undefined值,但它通过了每一个测试。事实上,即使在同一个expect中,它也会传递一个 值,而使另一个失败。他们都有undefined! -
啊!我是个白痴。问题实际上不是
undefined,整个对象都丢失了。 Jest 正在按预期工作。 -
嗯,我也学到了一些新东西。不知道
toEqual会与对象松散匹配。 -
是的,他们添加了
toStrictEqual进行严格检查!
标签: javascript jestjs