Categories
JavaScript Answers

How to reset or clear a spy in Jest?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *