Sometimes, we want to reset Jest mock functions calls count before every test with JavaScript.
in this article, we’ll look at how to reset Jest mock functions calls count before every test with JavaScript.
How to reset Jest mock functions calls count before every test with JavaScript?
To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.
For instance, we write
afterEach(() => {
local.getData.mockClear();
});
to call local.getData.mockClear to clear the mocked local.getData method after each test by calling it in the afterEach callback.
We can use
afterEach(() => {
jest.clearAllMocks();
});
to call jest.clearAllMocks to clear all mocks after each test.
Conclusion
To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.