众所周知,expect 被 jest 取代了。 expect 的一些属性也会改变。其中之一是 toContain,它被称为 toInclude。你会在这里找到它:https://github.com/skovhus/jest-codemods/blob/master/src/transformers/expect.js
我的问题是当我尝试使用 toContain 来检查一个对象是否与另一个对象匹配时,它显示错误。但早些时候使用 toInclude 它只是一行代码。因此,作为 toInclude 的替代品,我发现它有所不同,并不完全相同。
这个数组工作正常。
expect([2,3,4]).toContain(4);
但是当我使用对象时,出现了这个错误
expect({
name : 'Adil',
age : 23
}).toContain({
age : 23
});
这是错误
Error: expect(object).toContain(value)
Expected object:
{"age": 23, "name": "Adil"}
To contain value:
{"age": 23}
最佳答案
.toContain
用于 checking that an item is in an array
如果你想检查一个对象的属性值,那么你可以使用.toHaveProperty
- here are the docs
所以你的例子是
expect({
name : 'Adil',
age : 23
}).toHaveProperty('age', 23);
...或者为了避免学习另一个你可以做的匹配器:
expect({
name : 'Adil',
age : 23
}.age).toBe(23);
关于javascript - expect 中 toContain() 的实际用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46742842/