预期数组但在Jest中收到了数组 [英] Expected Array but received array in Jest

查看:431
本文介绍了预期数组但在Jest中收到了数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

[
    {
        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()是否有修复程序?

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

推荐答案

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

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

这篇关于预期数组但在Jest中收到了数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆