#resetAllMocks, restoreAllMocks, clearAllMocks don't reset the call count to mock functions

5 messages · Page 1 of 1 (latest)

desert latch
#

I've added resetAllMocks restoreAllMocks and clearAllMocks to the beforeEach and afterEach methods. However, the call count to these mocks isn't being reset at the end of every test.

Can I get some help with this?

#

For more context: I'm trying to mock a rejected value by passing in new Error('Failed')

marsh ether
#

show us your test suite

desert latch
#

Sorry. Actually, the calls are being reset, but when I'm mocking the rejected value and expecting the function toHaveBeenCalled 1 time, it just shows me that it was called 0 times.

it('should throw an Error when getSomething fails', async () => {
      const startDate = new Date();
      const endDate = new Date();
      jest.spyOn(service1, 'func3').mockResolvedValueOnce([oneBranchMock]);
      jest
        .spyOn(testRepo, 'func2')
        .mockRejectedValueOnce(new Error('Failed'));
      expect(
        service.getSomething(token, startDate, endDate, [], [], []),
      ).rejects.toBeInstanceOf(Error);
      expect(service1.func3).toHaveBeenCalledTimes(1);
      expect(testRepo.func2).toHaveBeenCalledTimes(1); // this is the assertion that is failing
      expect(testRepo2.findBy).toHaveBeenCalledTimes(0);
    });
lone whale
#
  1. await theexpect().rejects... call.
  2. it would be helpful to know actual methods and the actual service
  3. are you certain that the testRepo.func2 is getting called?