Test your routes end to end with node:test and fetch on an ephemeral port, each test getting its own in-memory SQLite database.
Open this lesson in KodokonNode 20 ships everything you need: node:test as the runner, node:assert/strict for assertions, global fetch as the HTTP client. Zero test dependencies. And this is where the architecture from the first lesson pays off: createApp({ db }) accepts an injected database, so each test can mount a complete, isolated API in a few milliseconds.
node --test
node --test --watchTo test the routes end to end, there's no need to mock Express: mount a real server on port 0 - the system assigns a free port - then hit it with fetch. You exercise exactly what a real client will see: routing, middlewares, serialization, status codes. It's the spirit of supertest, without the dependency.
import { once } from 'node:events';
export async function startTestServer(app) {
const server = app.listen(0);
await once(server, 'listening');
const { port } = server.address();
const url = `http://127.0.0.1:${port}`;
const close = () =>
new Promise((resolve) => {
server.close(resolve);
});
return { url, close };
}The test database: SQLite in memory (:memory:). Each test builds its own fresh, blank database via createDb, which applies the schema, then discards it on the way out - no data shared between tests, no cleanup script. t.after guarantees the server is closed even when an assertion fails.
import test from 'node:test';
import assert from 'node:assert/strict';
import { createDb } from '../src/db/connection.js';
import { createApp } from '../src/app.js';
import { startTestServer } from './helpers.js';
test('POST /api/users creates a user', async (t) => {
const db = createDb(':memory:');
const app = createApp({ db });
const server = await startTestServer(app);
t.after(() => server.close());
const res = await fetch(`${server.url}/api/users`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
email: 'ada@example.com',
name: 'Ada',
}),
});
assert.equal(res.status, 201);
const body = await res.json();
assert.equal(body.email, 'ada@example.com');
});app.listen(0)?:memory: database created in each test?t.after rather than at the end of the test body?