app.inject()

Simulates an HTTP request in-process for fast testing without a network socket.

Since Fastify 5 Spec ↗

Syntax

app.inject(options): Promise<LightMyRequestResponse>

Parameters

NameTypeRequiredDescription
options object No Object with method, url, headers, payload, and query.

Returns

Promise<Response> — A response with statusCode, body, json(), and headers.

Examples

import { test } from 'node:test';
import assert from 'node:assert';
import Fastify from 'fastify';

test('GET /health', async () => {
  const app = Fastify();
  app.get('/health', async () => ({ ok: true }));

  const res = await app.inject({ method: 'GET', url: '/health' });

  assert.equal(res.statusCode, 200);
  assert.deepEqual(res.json(), { ok: true });
});

Notes

inject() is built on light-my-request and calls ready() automatically. It is the recommended way to unit test routes without opening a port.