.toHaveBeenNthCalledWith(nthCall, arg1, arg2, ….)

Also under the alias: .nthCalledWith(nthCall, arg1, arg2, …)

If you have a mock function, you can use .toHaveBeenNthCalledWith to test what arguments it was nth called with. For example, let's say you have a drinkEach(drink, Array<flavor>) function that applies f to a bunch of flavors, and you want to ensure that when you call it, the first flavor it operates on is 'lemon' and the second one is 'octopus'. You can write:

  1. test('drinkEach drinks each drink', () => {
  2. const drink = jest.fn();
  3. drinkEach(drink, ['lemon', 'octopus']);
  4. expect(drink).toHaveBeenNthCalledWith(1, 'lemon');
  5. expect(drink).toHaveBeenNthCalledWith(2, 'octopus');
  6. });

Note: the nth argument must be positive integer starting from 1.