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()
?
You should use
toEqual
(nottoBe
) to compare objects and arrays. UsetoBe
for scalar data types only. If you like to check the response data type usetypeof
operator