Categories
JavaScript Answers

How to reset or clear a spy in Jest and JavaScript?

Spread the love

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.

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 *