Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected Array but received array in Jest

I created unit (async) test in Jest. But when I get response from server:

[
    {
        name: "My name"
    },
    {
        name: "Another name"
    }
]

and test it:

test('Response from server', () => {
    get('my-url').end(error, response) => {
        expect(response.body).toBe(expect.any(Array))
    }
})

some error occurs:

Comparing two different types of values. Expected Array but received array.

It's working when I use expect(response.body).any(Array). But is there any fix for expect.toBe()?

like image 310
Vesmy Avatar asked Sep 16 '17 16:09

Vesmy


1 Answers

You should use toEqual (not toBe) to compare objects and arrays. Use toBe for scalar data types only. If you like to check the response data type use typeof operator

like image 149
Alexander Elgin Avatar answered Sep 26 '22 01:09

Alexander Elgin