To fix Jest mock the same function twice with different arguments, we call mockReturnValueOnce
.
For instance, we write
myMock.mockReturnValueOnce(10).mockReturnValueOnce("x").mockReturnValue(true);
to call mockReturnValueOnce
on the myMock
mock object.
Then we call mockReturnValue
again to return a different mock value for a different call.
Finally, we call mockReturnValue
to return a 3rd value for myMock
.
Then we can check them with
expect(mock).toHaveBeenNthCalledWith(1, "1st call args");
expect(mock).toHaveBeenNthCalledWith(2, "2nd call arg 1", "2nd call arg 2");
to check with the arguments that mock
is called with in different calls.