Mocking Dependencies
Mocking Dependencies — why it matters
Mocking replaces a dependency at a clearly defined boundary so a unit test can control its responses. Interfaces describe the dependency shape; test doubles supply behavior. Too much mocking can make tests pass even when the real components are incompatible, so keep integration coverage too.
What you’ll learn
- Explain mocking dependencies and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
The production function receives a Clock dependency rather than reading global time directly. The test injects deterministic time 100, making both expiration branches reproducible.
A mock verifies behavior under its simulated assumptions, not actual network availability or database guarantees.
Follow the concept flow
Run the primary TypeScript example
Save as mocking-dependencies.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
type Clock = { now(): number };
function isExpired(deadline: number, clock: Clock): boolean {
return clock.now() > deadline;
}
const fakeClock: Clock = { now: () => 100 };
console.log(isExpired(90, fakeClock));
console.log(isExpired(110, fakeClock));Verified output
true
falseCompile: npx tsc --strict --target es2022 --module commonjs mocking-dependencies.ts; run: node mocking-dependencies.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
The production function receives a Clock dependency rather than reading global time directly. The test injects deterministic time 100, making both expiration branches reproducible.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Common mistake and correction
A mock verifies behavior under its simulated assumptions, not actual network availability or database guarantees.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Create a fake email sender that records messages; test whether the correct recipient receives a call.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
What to remember
Mocking replaces a dependency at a clearly defined boundary so a unit test can control its responses.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.