Express integration testing
Framework lane: Express 5.x (Supertest required for integration test). Exact supported patch, dependency lock, advisory review and execution are NOT RUN. The framework code is a static candidate; do not install dependencies or expose a server automatically. The local companion uses Node.js 22.16.0 and requires no third-party packages.
Framework example — staged
import express from 'express';import request from 'supertest';import assert from 'node:assert/strict';const app = express();app.get('/health', (req,res) => res.json({ ready: true }));app.use((req,res) => res.sendStatus(404));const response = await request(app).get('/health').expect(200);assert.deepEqual(response.body, { ready: true });await request(app).get('/missing').expect(404);Expected behavior (not observed): Both HTTP assertions should pass; a JSON response with ready:false or wrong status should fail. Requires an approved isolated environment with local listener support.
How it works
Use app factories and fresh data per test; separate a service unit test from actual routing, parsing and error middleware. Supertest can exercise an Express app and handle its temporary listener. The example requires a reviewed Supertest dependency and Express 5. In a controlled environment test missing records, invalid input and deny cases, not just success. A passing pure assertion below is not evidence that Express is running.
Practice the underlying idea without dependencies
import assert from 'node:assert/strict';const response = { status: 200, body: { ready: true } };assert.equal(response.status, 200);assert.deepEqual(response.body, { ready: true });assert.throws(()=>assert.deepEqual({ready:false}, response.body));console.log('contract assertions passed');Run node main.mjs in a disposable directory. Expected output:
contract assertions passedThis tests the illustrated JavaScript behavior only, not the framework.
Common mistake
Checking only the response body can miss a wrong status code. Conversely, testing an object directly does not exercise middleware or HTTP encoding.
Apply it yourself
Add test cases for malformed JSON, unsupported content type, and an unavailable repository; write expected status and safe body for each.
Further reading
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Express integration testing — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
Which test exercises actual Express routing?
What should a test check in addition to JSON body?
Why create a fresh app or fixture per test?