Test doubles

Hand fakes

A fake is a tiny working substitute for a slow dependency. Instead of a real database, pass a small class that stores rows in memory behind the same method names. The product code cannot tell the difference, and the test runs in milliseconds:

import assert from "node:assert/strict"; interface Store { save(text: string): void; all(): string[]; } class MemoryStore implements Store { private rows: string[] = []; save(text: string): void { this.rows.push(text); } all(): string[] { return this.rows; } } const store: Store = new MemoryStore(); store.save("a"); assert.equal(store.all().length, 1); console.log("store rows: " + store.all().length);

store rows: 1

The interface is the contract and the fake is one honest implementation of it. Swap the real store for MemoryStore in tests, and every read and write stays fast and repeatable.

Stub functions

A stub is a function that returns a canned answer. When the product needs a network fetch, hand it a stub that returns fixed data, and test the logic around the call instead of the network:

import assert from "node:assert/strict"; function stubUser(): { name: string } { return { name: "Ada" }; } function greet(load: () => { name: string }): string { return "hi " + load().name; } const text: string = greet(stubUser); assert.equal(text, "hi Ada"); console.log("greeting: " + text);

greeting: hi Ada

greet takes any loader with the right shape, so the stub slides in without special wiring. The greeting logic is tested, and the network never wakes up.

Record calls

A spy records how it was called. Wrap the dependency in a function that pushes each argument into an array, then assert on that array after the run. The test proves not only the result but the conversation:

import assert from "node:assert/strict"; const calls: string[] = []; const spy = (text: string): void => { calls.push(text); }; spy("hello"); assert.deepStrictEqual(calls, ["hello"]); console.log("calls: " + calls.join(", "));

calls: hello

The calls array is the recorded history: its length tells how often the spy ran, and its items tell with what. Start the array empty in every test so histories never mix.

Control time simply

Time makes tests flaky when code reads the real clock. Control it simply: pass the current time as a plain number argument instead of calling for the time deep inside the logic. The test sends morning and afternoon values, and every run repeats exactly:

import assert from "node:assert/strict"; function greeting(hour: number): string { if (hour < 12) { return "morning"; } return "day"; } assert.equal(greeting(9), "morning"); assert.equal(greeting(15), "day"); console.log("9: " + greeting(9)); console.log("15: " + greeting(15));

9: morning
15: day

No timers are mocked and nothing sleeps. Pure values in, plain strings out, and midnight never surprises the suite at midnight.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a hand fake in a test?

    Answer

    A small working substitute for a slow dependency

    A fake implements the same contract in memory, so the product code cannot tell it from the real dependency and the test stays fast.

  2. What is a stub?

    Answer

    A function that returns a canned answer

    A stub replaces a dependency with fixed data, so the logic around the call is tested without the network or a database.

  3. What does a spy record?

    Answer

    How it was called, usually the arguments of every call

    A spy pushes each argument into an array, and the test asserts on that history to prove the conversation, not only the result.

  4. How does this lesson keep time out of a test?

    Answer

    It passes the current time as a plain number argument

    The function takes the hour as an argument, so the test sends fixed values and never reads the real clock.

  5. What does this program print? import assert from "node:assert/strict"; interface Store { save(text: string): void; all(): string[]; } class MemoryStore implements Store { private rows: string[] = []; save(text: string): void { this.rows.push(text); } all(): string[] { return this.rows; } } const store: Store = new MemoryStore(); store.save("a"); assert.equal(store.all().length, 1); console.log(store.all().join(", "));

    Answer

    a

    The fake saved one row with the text a, and joining that one row prints the text itself.

  6. What does this program print? import assert from "node:assert/strict"; function stubUser(): { name: string } { return { name: "Ada" }; } function greet(load: () => { name: string }): string { return "hi " + load().name; } assert.equal(greet(stubUser), "hi Ada"); console.log(greet(stubUser));

    Answer

    hi Ada

    The stub returns the fixed user, and greet builds the same greeting from it that the real loader would give.

  7. What does this program print? import assert from "node:assert/strict"; const calls: string[] = []; const spy = (text: string): void => { calls.push(text); }; spy("hello"); assert.deepStrictEqual(calls, ["hello"]); console.log(calls.join(", "));

    Answer

    hello

    The spy recorded one argument, and joining the history prints that argument.

  8. What does this program print? import assert from "node:assert/strict"; function greeting(hour: number): string { if (hour < 12) { return "morning"; } return "day"; } assert.equal(greeting(9), "morning"); assert.equal(greeting(15), "day"); console.log("9: " + greeting(9)); console.log("15: " + greeting(15));

    Answer

    9: morning / 15: day

    Nine is before noon and fifteen is after it, so the two fixed hours produce the two printed lines.

  9. What does the compiler say about this program? interface Store { save(text: string): void; } class MemoryStore implements Store { save(text: number): void { console.log(text); } }

    Answer

    error TS2416: Property 'save' in type 'MemoryStore' is not assignable to the same property in base type 'Store'.

    The class promises to implement Store but gives save a number parameter, so the method is not assignable to the interface method.

  10. Why is greeting(hour) easier to test than a function that calls Date.now() itself?

    Answer

    The test chooses the hour, so the result is the same at any real time

    The caller supplies the time, so morning and afternoon are values in the test rather than a race against the clock.

Article author: Arthur Isaev