Function would be called with:
{ items: [1, 2, 3]}
expect(mockedFunction).toHaveBeenCalledWith(
expect.objectContaining({
items: expect.toBeLengthOf(3) // Pseudocode, this function does not exist here
}),
);
.mock
object on mocked function has .calls
list on it. So
expect(
mockedFunction.mock.calls[mockedFunction.mock.calls.length - 1][0].items
).toHaveLength(3);
will validate length of items
property on first(0-based numeration) argument for last time(mockedFunction.mock.calls.length - 1
) it has been called.
Also you may consider using expect.anything()
as a placeholder:
expect(mockedFunction).toHaveBeenLastCalledWith(
expect.objectContaining({
items: [expect.anything(), expect.anything(), expect.anything()]
})
)
But it may be better to use a mock and return the same values consistently - this will allow you to validate the contents of the array rather than just its length
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With