Sometimes, we want to reset or clear a spy in Jest and JavaScript.
In this article, we’ll look at how to reset or clear a spy in Jest and JavaScript.
How to reset or clear a spy in Jest and JavaScript?
To reset or clear a spy in Jest and JavaScript, we call the jest.clearAllMocks
method.
For instance, we write
afterEach(() => {
jest.clearAllMocks();
});
to call jest.clearAllMocks
in the afterEach
callback to clear all spies after each test is run.
Conclusion
To reset or clear a spy in Jest and JavaScript, we call the jest.clearAllMocks
method.