预期数组但在 Jest 中收到数组

IT技术 javascript arrays reactjs jestjs
2021-04-27 11:44:24

我在 Jest 中创建了单元(异步)测试。但是当我从服务器得到响应时:

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

并测试它:

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

出现一些错误:

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

当我使用expect(response.body).any(Array). 但是有什么解决办法expect.toBe()吗?

1个回答

您应该使用toEqual(not toBe) 来比较对象和数组。使用toBe仅标量数据类型。如果您想检查响应数据类型,请使用typeof运算符