.toHaveBeenCalledTimes(number)

Also under the alias: .toBeCalledTimes(number)

Use .toHaveBeenCalledTimes to ensure that a mock function got called exact number of times.

For example, let's say you have a drinkEach(drink, Array<flavor>) function that takes a drink function and applies it to array of passed beverages. You might want to check that drink function was called exact number of times. You can do that with this test suite:

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