Javascript 有没有一种方法可以使用jests to havenCalledWith检查参数数组长度?
函数将通过以下方式调用:Javascript 有没有一种方法可以使用jests to havenCalledWith检查参数数组长度?,javascript,typescript,jestjs,Javascript,Typescript,Jestjs,函数将通过以下方式调用: {items:[1,2,3]} `expect(mockedFunction).toHaveBeenCalledWith( expect.objectContaining({ items: expect.toBeLengthOf(3) // Pseudocode, this function does not exist here }), );` 模拟函数上有.calls列表。所以 expect( mockedFunction.mock.calls
{items:[1,2,3]}
`expect(mockedFunction).toHaveBeenCalledWith(
expect.objectContaining({
items: expect.toBeLengthOf(3) // Pseudocode, this function does not exist here
}),
);`
模拟函数上有.calls
列表。所以
expect(
mockedFunction.mock.calls[mockedFunction.mock.calls.length - 1][0].items
).toHaveLength(3);
将验证上次调用(mockedFunction.mock.calls.length-1
)时第一个(基于0的计数)参数上的项的长度
也可以考虑使用占位符:
expect(mockedFunction).toHaveBeenLastCalledWith(
expect.objectContaining({
items: [expect.anything(), expect.anything(), expect.anything()]
})
)
但我建议您检查是否可以模拟某些东西(以便返回相同的值一致性)来验证参数本身,而不仅仅是检查内部某个地方的数组长度