Menu

1 / 2

Testing — Fastify with TypeScript (part 15/18)

DEV Community: fastify·Antonio Tripodi·9 days ago
#9n0IyysU
#devtofastify#devto#dev#test#assert
Long article (5000 words) — no AI summary yet.
Reading 0:00
15s threshold

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 ??…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.