As we all know expect replaced with jest. Some property of expect also changes. One of them is toContain which was known as toInclude. You will find it here : https://github.com/skovhus/jest-codemods/blob/master/src/transformers/expect.js
My problem is when i am trying to use toContain in order to check whether a object match with another object,it showing me error. But earlier with toInclude it was just one line code. So as a replacement of toInclude I find it different,not exact the same.
This array works fine.
expect([2,3,4]).toContain(4);
But when i go with object,this error come up
expect({
name : 'Adil',
age : 23
}).toContain({
age : 23
});
This is the error
Error: expect(object).toContain(value)
Expected object:
{"age": 23, "name": "Adil"}
To contain value:
{"age": 23}
.toContain
is for checking that an item is in an array
If you want to check the value of an object's property then you can use .toHaveProperty
- here are the docs
so your example would be
expect({
name : 'Adil',
age : 23
}).toHaveProperty('age', 23);
... or to avoid learning another matcher you could just do:
expect({
name : 'Adil',
age : 23
}.age).toBe(23);
Its used when you want to check the existence of an item within an array. Its similar to python's x in [1,2,3]
. Note that it will not give you the index of the first occurrence. It will just return a boolean
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With