.toHaveNthReturnedWith(nthCall, value)

Also under the alias: .nthReturnedWith(nthCall, value)

Use .toHaveNthReturnedWith to test the specific value that a mock function returned for the nth call. If the nth call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

For example, let's say you have a mock drink that returns the name of the beverage that was consumed. You can write:

  1. test('drink returns expected nth calls', () => {
  2. const beverage1 = {name: 'La Croix (Lemon)'};
  3. const beverage2 = {name: 'La Croix (Orange)'};
  4. const drink = jest.fn(beverage => beverage.name);
  5. drink(beverage1);
  6. drink(beverage2);
  7. expect(drink).toHaveNthReturnedWith(1, 'La Croix (Lemon)');
  8. expect(drink).toHaveNthReturnedWith(2, 'La Croix (Orange)');
  9. });

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