Repository: fastify/fastify
File: test/decorator.test.js (JavaScript)

Code before:
    })

    done()
  })

  fastify.get('/no', (req, reply) => {
    t.assert.ok(!reply.test)
    reply.send({ hello: 'world' })
  })

  const fastifyServer = await fastify.listen({ port: 0 })
  t.after(() => fastify.close())

  const result1 = await fetch(`${fastifyServer}/yes`)
  t.assert.ok(result1.ok)
  t.assert.strictEqual(result1.status, 200)
  const body1 = await result1.text()
  t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
  t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' })

  const result2 = await fetch(`${fastifyServer}/no`)
  t.assert.ok(result2.ok)
  t.assert.strictEqual(result2.status, 200)
  const body2 = await result2.text()
  t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
  t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'world' })
})

test('decorateReply as plugin (inside .after)', async t => {
  t.plan(10)
  const fastify = Fastify()

  fastify.register((instance, opts, done) => {
    instance.register(fp((i, o, n) => {
      instance.decorateReply('test', 'test')
      n()
    })).after(() => {
      instance.get('/yes', (req, reply) => {
        t.assert.ok(reply.test)
        reply.send({ hello: 'world' })
      })
    })
    done()
  })

  fastify.get('/no', (req, reply) => {
    t.assert.ok(!reply.test)
    reply.send({ hello: 'world' })
  })

  const fastifyServer = await fastify.listen({ port: 0 })

Review comment (suggestion):
You can also create an array of fixtures when you have repetitive assertions:
```js
const endpoints = [
  { path: '/yes', expectedBody: { hello: 'world' } },
  { path: '/no', expectedBody: { hello: 'world' } }
]

for (const { path, expectedBody } of endpoints) {
  const result = await fetch(`${fastifyServer}${path}`)
  t.assert.ok(result.ok)
  t.assert.strictEqual(result.status, 200)
  const body = await result.text()
  t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
  t.assert.deepStrictEqual(JSON.parse(body), expectedBody)
}
```


Code after:
    const body = await result.text()
    t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
    t.assert.deepStrictEqual(JSON.parse(body), expectedBody)
  }
}

test('decorateReply inside register', async (t) => {
  t.plan(10)
  const fastify = Fastify()

  fastify.register((instance, opts, done) => {
    instance.decorateReply('test', 'test')

    instance.get('/yes', (req, reply) => {
      t.assert.ok(reply.test, 'test exists')
      reply.send({ hello: 'world' })
    })

    done()
  })

  fastify.get('/no', (req, reply) => {
    t.assert.ok(!reply.test)
    reply.send({ hello: 'world' })
  })

  const fastifyServer = await fastify.listen({ port: 0 })
  t.after(() => fastify.close())

  await runTests(t, fastifyServer)
})

test('decorateReply as plugin (inside .after)', async t => {
  t.plan(10)
  const fastify = Fastify()

  fastify.register((instance, opts, done) => {
    instance.register(fp((i, o, n) => {
      instance.decorateReply('test', 'test')
      n()
    })).after(() => {
      instance.get('/yes', (req, reply) => {
        t.assert.ok(reply.test)
        reply.send({ hello: 'world' })
      })
    })
    done()
  })

  fastify.get('/no', (req, reply) => {
    t.assert.ok(!reply.test)