Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the actual use of toContain() in expect?

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}
like image 484
Adil Chowdhury Avatar asked Oct 14 '17 09:10

Adil Chowdhury


2 Answers

.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);
like image 147
Billy Reilly Avatar answered Oct 03 '22 02:10

Billy Reilly


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

like image 32
Alan Avatar answered Oct 03 '22 02:10

Alan