.toHaveLastReturnedWith(value)

Also under the alias: .lastReturnedWith(value)

Use .toHaveLastReturnedWith to test the specific value that a mock function last returned. If the last 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 La Croix (Orange) last', () => {
  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).toHaveLastReturnedWith('La Croix (Orange)');
  8. });