Expected Array but received array in Jest

2019-07-30 08:38发布

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()?

1条回答
▲ chillily
2楼-- · 2019-07-30 09:24

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

查看更多
登录 后发表回答