Fastify

Fastify inject()

Hooks let Fastify perform work at named lifecycle stages. Injection tests run requests through the app without an externally listening HTTP server. Use onRequest for work before body parsing and preHandler for work after validation. A hook can finish a response early; avoid mixing async hooks with callback completion. app.inject waits for plugin readiness and returns a response object; close the instance after tests. Injection exercises app behavior but does not prove socket, proxy or TLS behavior. Static example only; runtime NOT RUN. Source: https://fastify.dev/docs/v5.6.x/Reference/Hooks/.

Formal Syntax & Structure

nodejs syntaxnodejs
await app.inject({ method, url, payload })

Working Example

Example Snippetnodejs
import Fastify from 'fastify';const app = Fastify();app.addHook('onRequest', async (req,reply) => { reply.header('X-Lab','demo'); });app.get('/status', async () => ({ready:true}));try {  const result = await app.inject('/status');  console.log(result.statusCode, result.headers['x-lab'], result.body);} finally { await app.close(); }
Node.js Reference