Home / File/ hooks-async.test.js — fastify Source File

hooks-async.test.js — fastify Source File

Architecture documentation for hooks-async.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { Readable } = require('node:stream')
const { test, describe } = require('node:test')
const Fastify = require('../fastify')
const fs = require('node:fs')
const { sleep } = require('./helper')
const { waitForCb } = require('./toolkit')

process.removeAllListeners('warning')

test('async hooks', async t => {
  t.plan(20)
  const fastify = Fastify({ exposeHeadRoutes: false })
  fastify.addHook('onRequest', async function (request, reply) {
    await sleep(1)
    request.test = 'the request is coming'
    reply.test = 'the reply has come'
    if (request.raw.method === 'DELETE') {
      throw new Error('some error')
    }
  })

  fastify.addHook('preHandler', async function (request, reply) {
    await sleep(1)
    t.assert.strictEqual(request.test, 'the request is coming')
    t.assert.strictEqual(reply.test, 'the reply has come')
    if (request.raw.method === 'HEAD') {
      throw new Error('some error')
    }
  })

  fastify.addHook('onSend', async function (request, reply, payload) {
    await sleep(1)
    t.assert.ok('onSend called')
  })

  const completion = waitForCb({
    steps: 6
  })
  fastify.addHook('onResponse', async function (request, reply) {
    await sleep(1)
    t.assert.ok('onResponse called')
    completion.stepIn()
  })

  fastify.get('/', function (request, reply) {
    t.assert.strictEqual(request.test, 'the request is coming')
    t.assert.strictEqual(reply.test, 'the reply has come')
    reply.code(200).send({ hello: 'world' })
  })

  fastify.head('/', function (req, reply) {
    reply.code(200).send({ hello: 'world' })
  })

  fastify.delete('/', function (req, reply) {
    reply.code(200).send({ hello: 'world' })
  })

// ... (1040 more lines)

Frequently Asked Questions

What does hooks-async.test.js do?
hooks-async.test.js is a source file in the fastify codebase, written in javascript.
Where is hooks-async.test.js in the architecture?
hooks-async.test.js is located at test/hooks-async.test.js (directory: test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free