Repository: fastify/fastify
File: test/hooks-async.test.js (JavaScript)

Code before:
  })

  fastify.inject({
    url: '/',
    method: 'GET'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    testDone()
  })
})

describe('Should log a warning if is an async function with `done`', () => {
  test('2 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onRequestAbort', async (req, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }
  })

  test('3 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onRequest', async (req, reply, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }
  })

  test('4 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onSend', async (req, reply, payload, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }
    try {

Review comment (bug):
For this type of test, counting assertions is irrelevant.
If `t.assert.fail` is not called, the catch block is executed.

Code after:
    t.assert.fail('we should not be here')
  })

  fastify.inject({
    url: '/',
    method: 'GET'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    testDone()
  })
})

describe('Should log a warning if is an async function with `done`', () => {
  test('2 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onRequestAbort', async (req, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }
  })

  test('3 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onRequest', async (req, reply, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }
  })

  test('4 arguments', t => {
    const fastify = Fastify()

    try {
      fastify.addHook('onSend', async (req, reply, payload, done) => {
        t.assert.fail('should have not be called')
      })
    } catch (e) {
      t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
      t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
    }