2. Spying, stubbing and function assertions

This section of The Jest Handbook is dedicated to improving your understanding of Jest’s rich spying, stubbing and mocking functionality.

You will learn to:

  • set, clear and reset mocks, stubs and spies
  • assert over spies and mocks
  • different approaches to stubbing global objects and testing subclasses
  • generate mock objects in JavaScript
  • intercept CommonJS and ESM imports

What are spies, mocks and stubs?

Spies, mocks and stubs fall into the category of test doubles. We’ll use them to check that a function gets called in a test.

We’ll also see how to do setup/teardown and assertions over stubs and spies with Jest.

Jump to table of contents