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

route-hooks.test.js — fastify Source File

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

Entity Profile

Relationship Graph

Source Code

'use strict'

const { Readable } = require('node:stream')
const { test } = require('node:test')
const Fastify = require('../')

process.removeAllListeners('warning')

function endRouteHook (doneOrPayload, done, doneValue) {
  if (typeof doneOrPayload === 'function') {
    doneOrPayload(doneValue)
  } else {
    done(doneValue)
  }
}

function testExecutionHook (hook) {
  test(`${hook}`, (t, testDone) => {
    t.plan(3)
    const fastify = Fastify()

    fastify.post('/', {
      [hook]: (req, reply, doneOrPayload, done) => {
        t.assert.ok('hook called')
        endRouteHook(doneOrPayload, done)
      }
    }, (req, reply) => {
      reply.send(req.body)
    })

    fastify.inject({
      method: 'POST',
      url: '/',
      payload: { hello: 'world' }
    }, (err, res) => {
      t.assert.ifError(err)
      const payload = JSON.parse(res.payload)
      t.assert.deepStrictEqual(payload, { hello: 'world' })
      testDone()
    })
  })

  test(`${hook} option should be called after ${hook} hook`, (t, testDone) => {
    t.plan(3)
    const fastify = Fastify()
    const checker = Object.defineProperty({ calledTimes: 0 }, 'check', {
      get: function () { return ++this.calledTimes }
    })

    fastify.addHook(hook, (req, reply, doneOrPayload, done) => {
      t.assert.strictEqual(checker.check, 1)
      endRouteHook(doneOrPayload, done)
    })

    fastify.post('/', {
      [hook]: (req, reply, doneOrPayload, done) => {
        t.assert.strictEqual(checker.check, 2)
        endRouteHook(doneOrPayload, done)
      }
    }, (req, reply) => {
// ... (576 more lines)

Domain

Subdomains

Frequently Asked Questions

What does route-hooks.test.js do?
route-hooks.test.js is a source file in the fastify codebase, written in javascript. It belongs to the CoreKernel domain, InstanceFactory subdomain.
What functions are defined in route-hooks.test.js?
route-hooks.test.js defines 3 function(s): endRouteHook, testBeforeHandlerHook, testExecutionHook.
Where is route-hooks.test.js in the architecture?
route-hooks.test.js is located at test/route-hooks.test.js (domain: CoreKernel, subdomain: InstanceFactory, directory: test).

Analyze Your Own Codebase

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

Try Supermodel Free