在jest中的对象比较中使用expect.any(String)

问题描述 投票:0回答:2

我有如下代码

    expect(insertedData).toEqual({
                id: expect.any(String),
                clientId: 'client1',
                ...

我希望 id 应该匹配任何字符串,但我得到的结果是

不知道我错过了什么。

我也像下面一样使用仍然相同的错误

expect(insertedData).toEqual(expect.objectContaining({
javascript unit-testing jestjs
2个回答
0
投票

这发生在我身上。事实证明,问题不在于 Expect.any() 的字段,而在于其他字段。似乎那里的差异很糟糕。


-1
投票

你的表达问题依赖于第一个期望,即

toEqual()

期望应该使用toMatchObject,例如:

expect(insertedData).toMatchObject({...
© www.soinside.com 2019 - 2024. All rights reserved.