#resetAllMocks, restoreAllMocks, clearAllMocks don't reset the call count to mock functions
5 messages · Page 1 of 1 (latest)
For more context: I'm trying to mock a rejected value by passing in new Error('Failed')
show us your test suite
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);
});
- await the
expect().rejects...call. - it would be helpful to know actual methods and the actual service
- are you certain that the
testRepo.func2is getting called?