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.
A Jest mock is a spy that replaces a real function, recording every call, its arguments, and context, without executing the original logic. This is crucial for unit tests, letting you verify a callback was invoked or force a dependency to return a specific value. This isolates your test from external factors like networks. The footgun is forgetting mocks are empty by default; you must explicitly provide return values or a mock implementation if your test depends on them.
Read the original → jestjs.io
- #jest
- #testing
- #javascript
- #unit-testing
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.