Jest Mocks: Spies for Your Functions

A Jest mock is a spy that watches a function, recording calls without running the original logic. This lets you isolate code under test, verify interactions like callbacks, and control return values. The footgun is assuming mocks run real logic—they don't.
Why it exists
Unit tests must be fast, reliable, and focused on a single unit of code. If testing a component also requires a live API and a running database, the test becomes slow, brittle, and hard to debug. Mocking was created to sever these external dependencies, allowing you to test one piece of code in perfect isolation.
The mental model
Think of a mock function as a stunt double for your real function. The stunt double doesn't know the actor's real lines and won't perform the original function's logic. Instead, it stands in, ready to be observed. You can check that it was called, how it was called, and you can give it specific instructions for a scene, like what value to return for a particular test.
How it works
You create a mock with jest.fn(). This special function comes with a .mock property that acts as a logbook. This property tracks all interactions: mock.calls is an array of arguments for each call, mock.results is an array of return values, and mock.instances tracks new instantiations. In your tests, you inspect these arrays to assert that the function was used as expected. You can also control its behavior by chaining methods like .mockReturnValue('test-value') or .mockReturnValueOnce(10) to dictate what it returns when called.
When to use it
Use mocks to isolate the code you are testing from its dependencies. This is ideal for replacing functions with external side effects, like API calls or database queries. It's also essential for verifying interactions, such as confirming a callback function passed to a component is actually invoked with the correct arguments when a user clicks a button.
When not to use it
Avoid mocking everything. For integration tests, the goal is to see how real modules work together. Do not mock trivial, pure functions or implementation details internal to the code you're testing; test the public interface, not the internal machinery. Over-mocking leads to brittle tests that are tightly coupled to the implementation and break on minor refactors.
One canonical example
Imagine testing a processData function that uses a fetchData utility. To avoid a real network call, you mock fetchData. First, create the mock: const mockFetchData = jest.fn(). In your test, you can specify what it should return for one specific call: mockFetchData.mockReturnValueOnce({ user: 'test' }). Then, you call your function under test. Finally, you can assert that the mock was called once (expect(mockFetchData.mock.calls.length).toBe(1)) and that your function correctly handled the mock's return value. This isolates your test from the network.
Interview question
What is a fundamental characteristic of a Jest mock function?
- a.It replaces the original function, records interaction details, and can be configured to return specific values.Correct
- b.It is primarily used in integration tests to ensure real modules interact correctly.
- c.It executes the original function's logic while logging all calls and their results.
- d.It automatically provides realistic data for external dependencies without requiring explicit setup.
Why? this is the answer
A Jest mock replaces the original function's logic, acting as a stand-in that records how it was called and allows its return value to be controlled. Option C is incorrect because mocks explicitly do not run the original function's logic; they isolate the test from it.
Just read this? Test yourself on what you have been reading.
Read the original → jestjs.io
- #jest
- #testing
- #javascript
- #unit-testing
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles