How to reset or clear a spy in Jest?

Sometimes, we want to reset or clear a spy in Jest.

In this article, we’ll look at how to reset or clear a spy in Jest.

How to reset or clear a spy in Jest?

To reset or clear a spy in Jest, we call jest.clearAllMocks.

For instance, we write:

afterEach(() => {
  jest.clearAllMocks();
});

to call jest.clearAllMocks in the afterEach callback to clear all spies or mocks after each test is run.

Conclusion

To reset or clear a spy in Jest, we call jest.clearAllMocks.