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

Concept

Integration tests

An integration test starts the real server against a real database and sends HTTP requests, checking status, body and side effects. It is slower than a unit test and catches what unit tests cannot.

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 an integration test exercises that a unit test cannot
  2. Start the server against a test database and send a request
  3. Spot the test that depends on data from an earlier test

In practice

A few lines that show Integration tests doing its job.

JavaScript
import { test, before, after } from "node:test";
import assert from "node:assert/strict";
import { build } from "../src/app.js";

let app;
before(async () => { app = await build({ databaseUrl: process.env.TEST_DATABASE_URL }); });
after(() => app.close());

test("creates an order", async () => {
  const res = await app.inject({ method: "POST", url: "/api/v1/orders",
    payload: { items: [{ sku: "A1", quantity: 2 }] } });
  assert.equal(res.statusCode, 201);
  assert.match(res.headers.location, /\/orders\//);
});

The common mistake

Watch for

Tests that depend on data left by earlier tests. They pass in one order and fail in another; each test should create what it needs and the database should be reset between them.

What to know first

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

How it stays learned

Once a session shows you understand Integration 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 Integration tests.

Previous in the module← Unit tests
Next in the moduleTest databases →