Home / Function/ testBeforeHandlerHook() — fastify Function Reference

testBeforeHandlerHook() — fastify Function Reference

Architecture documentation for the testBeforeHandlerHook() function in route-hooks.test.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  1841381b_f7eb_1193_2637_825d837495cc["testBeforeHandlerHook()"]
  20e9500e_9530_50df_e580_26d847082774["route-hooks.test.js"]
  1841381b_f7eb_1193_2637_825d837495cc -->|defined in| 20e9500e_9530_50df_e580_26d847082774
  1423a7ac_9338_cc04_41d6_66f0234eced2["endRouteHook()"]
  1841381b_f7eb_1193_2637_825d837495cc -->|calls| 1423a7ac_9338_cc04_41d6_66f0234eced2
  style 1841381b_f7eb_1193_2637_825d837495cc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

test/route-hooks.test.js lines 182–390

function testBeforeHandlerHook (hook) {
  test(`${hook} option should be unique per route`, (t, testDone) => {
    t.plan(4)
    const fastify = Fastify()

    fastify.post('/', {
      [hook]: (req, reply, doneOrPayload, done) => {
        req.hello = 'earth'
        endRouteHook(doneOrPayload, done)
      }
    }, (req, reply) => {
      reply.send({ hello: req.hello })
    })

    fastify.post('/no', (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: 'earth' })
    })

    fastify.inject({
      method: 'POST',
      url: '/no',
      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 handle errors`, (t, testDone) => {
    t.plan(3)
    const fastify = Fastify()

    fastify.post('/', {
      [hook]: (req, reply, doneOrPayload, done) => {
        endRouteHook(doneOrPayload, done, new Error('kaboom'))
      }
    }, (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.strictEqual(res.statusCode, 500)
      t.assert.deepStrictEqual(payload, {
        message: 'kaboom',
        error: 'Internal Server Error',
        statusCode: 500
      })
      testDone()
    })
  })

  test(`${hook} option should handle throwing objects`, (t, testDone) => {
    t.plan(4)
    const fastify = Fastify()

    const myError = { myError: 'kaboom' }

    fastify.setErrorHandler(async (error, request, reply) => {
      t.assert.deepStrictEqual(error, myError, 'the error object throws by the user')
      return reply.code(500).send({ this: 'is', my: 'error' })
    })

    fastify.get('/', {

Domain

Subdomains

Frequently Asked Questions

What does testBeforeHandlerHook() do?
testBeforeHandlerHook() is a function in the fastify codebase, defined in test/route-hooks.test.js.
Where is testBeforeHandlerHook() defined?
testBeforeHandlerHook() is defined in test/route-hooks.test.js at line 182.
What does testBeforeHandlerHook() call?
testBeforeHandlerHook() calls 1 function(s): endRouteHook.

Analyze Your Own Codebase

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

Try Supermodel Free