.toHaveReturnedTimes(number)

Also under the alias: .toReturnTimes(number)

Use .toHaveReturnedTimes to ensure that a mock function returned successfully (i.e., did not throw an error) an exact number of times. Any calls to the mock function that throw an error are not counted toward the number of times the function returned.

For example, let's say you have a mock drink that returns true. You can write:

  1. test('drink returns twice', () => {
  2. const drink = jest.fn(() => true);
  3. drink();
  4. drink();
  5. expect(drink).toHaveReturnedTimes(2);
  6. });