Lesson 18 / 30
Testing Routes
Write fast tests for your Fastify routes.
In-process testing
Fastify can inject requests directly without starting a real server—ideal for unit and integration tests.
Testing with inject
Use fastify.inject() to send requests and verify responses.
import test from 'node:test';
import assert from 'node:assert';
import build from './app.js';
test('GET /health returns 200', async () => {
const fastify = await build();
const response = await fastify.inject({
method: 'GET',
url: '/health',
});
assert.equal(response.statusCode, 200);
assert.deepEqual(JSON.parse(response.body), { status: 'ok' });
await fastify.close();
});