1. Concept library
  2. Backend with Node.js
  3. Module 9: Testing APIs

Concept

Unit tests

A unit test checks one function or module in isolation, with its dependencies replaced, so it runs in milliseconds and points at exactly what broke. Node ships a test runner; no framework is required.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a unit test isolates and why it runs in milliseconds
  2. Write a unit test with Node's test runner
  3. Spot the glue code tested by mocking everything it calls

In practice

A few lines that show Unit tests doing its job.

JavaScript
import { test } from "node:test";
import assert from "node:assert/strict";
import { priceWithTax } from "../src/pricing.js";

test("adds tax and rounds to cents", () => {
  assert.equal(priceWithTax(1000, 0.0825), 1083);
});

test("rejects a negative rate", () => {
  assert.throws(() => priceWithTax(1000, -0.1), /rate/);
});

The common mistake

Watch for

Unit testing code that is mostly glue, such as a route handler, by mocking everything it calls. The test mirrors the implementation and passes while the real wiring is broken.

What to know first

Your roadmap puts these before Unit tests, unless you already know them.

How it stays learned

Once a session shows you understand Unit tests, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Unit tests.

Next in the moduleIntegration tests →