Testing
Fifteenth chapter of the "Fastify with TypeScript" series. Thanks to the app.ts / server.ts separation and the config overrides from chapter 4, testing Fastify is simple and fast: no network ports, no test framework to install, just simulated requests through Node's built-in test runner. We'll write the tests, then automate everything around them: lint, coverage and CI.
Fastify's inject() method simulates an HTTP request without opening a network port. The request goes through the whole lifecycle (hooks, validation, handler, serialization) and you get the response back as an object. It's the fastest way to test routes and plugins.
A test helper
Create test/helper.ts :
// test/helper.ts import { buildApp , type App } from '../src/app.ts' ;
export async function build ( ) : Promise < App > { const app = buildApp ( { logger : false } , { JWT_SECRET : 'test-secret-test-secret-test-secret-1234' , DATABASE_URL : process . env . DATABASE_URL ??…