Home / File/ hooks.on-ready.test.js — fastify Source File

hooks.on-ready.test.js — fastify Source File

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

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('../fastify')
const immediate = require('node:util').promisify(setImmediate)

test('onReady should be called in order', (t, done) => {
  t.plan(7)
  const fastify = Fastify()

  let order = 0

  fastify.addHook('onReady', function (done) {
    t.assert.strictEqual(order++, 0, 'called in root')
    t.assert.strictEqual(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
    done()
  })

  fastify.register(async (childOne, o) => {
    childOne.addHook('onReady', function (done) {
      t.assert.strictEqual(order++, 1, 'called in childOne')
      t.assert.strictEqual(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
      done()
    })

    childOne.register(async (childTwo, o) => {
      childTwo.addHook('onReady', async function () {
        await immediate()
        t.assert.strictEqual(order++, 2, 'called in childTwo')
        t.assert.strictEqual(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
      })
    })
  })

  fastify.ready(err => {
    t.assert.ifError(err)
    done()
  })
})

test('onReady should be called once', async (t) => {
  const app = Fastify()
  let counter = 0

  app.addHook('onReady', async function () {
    counter++
  })

  const promises = [1, 2, 3, 4, 5].map((id) => app.ready().then(() => id))

  const result = await Promise.race(promises)

  t.assert.strictEqual(result, 1, 'Should resolve in order')
  t.assert.strictEqual(counter, 1, 'Should call onReady only once')
})

test('async onReady should be called in order', async t => {
  t.plan(7)
  const fastify = Fastify()

// ... (362 more lines)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free