tezvyn:

Difference between jest.fn, jest.spyOn, and jest.mock in React

Source: jestjs.iointermediate

Tests whether you distinguish Jest's three mocking scopes. Good answers: jest.fn for standalone callbacks; jest.spyOn to observe existing methods; jest.mock to swap entire modules. Red flag: using them interchangeably or saying spyOn replaces modules.

Tests whether you understand the granularity and lifecycle of Jest's three core mocking primitives. A strong answer distinguishes jest.fn as a standalone mock for injected callbacks, jest.spyOn as a wrapper around existing methods that preserves original behavior by default, and jest.mock as a module-level replacement that hoists above imports. It pairs each with React scenarios: jest.fn for prop callbacks like onSubmit, jest.spyOn to verify a utility method was called, and jest.mock to stub child components or API modules.

Read the original → jestjs.io

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Difference between jest.fn, jest.spyOn, and jest.mock in React · Tezvyn