Mocking & Test Doubles in Node.js
Learn how to isolate the code under test by replacing real dependencies with mocks, stubs, and spies so your unit tests stay fast and deterministic.
The Problem with Real Dependencies
A unit test should check one piece of logic in isolation. But real code calls databases, APIs, the clock, and the file system — all slow, flaky, and unpredictable.
Test doubles replace those dependencies with controllable fakes.
Types of Test Doubles
Common terms you will hear:
- Stub: returns a canned value
- Spy: records how it was called
- Mock: a stub + spy with built-in expectations
- Fake: a lightweight working implementation
All lessons in this course
- Unit Testing with Jest
- Integration Testing API Endpoints
- Effective Debugging Techniques
- Mocking & Test Doubles in Node.js