ronantakizawa/codereview-bench huggingface dataset:
Repository: fastify/fastify
File: test/find-route.test.js (JavaScript)

Code before:
  })

  t.assert.strictEqual(fastify.findRoute({
    method: 'GET',
    url: '/books/:bookId'
  }), null)
})

test('findRoute should return the route when found', t => {
  t.plan(1)
  const fastify = Fastify()

  const handler = (req, reply) => reply.send(typeof req.params.artistId)

  fastify.get('/artists/:artistId', {
    schema: {
      params: { artistId: { type: 'integer' } }
    },
    handler
  })

  const route = fastify.findRoute({
    method: 'GET',
    url: '/artists/:artistId'
  })
  t.assert.deepStrictEqual(route.params.artistId, ':artistId')
})

test('findRoute should work correctly when used within plugins', (t, done) => {
  t.plan(1)
  const fastify = Fastify()
  const handler = (req, reply) => reply.send(typeof req.params.artistId)
  fastify.get('/artists/:artistId', {
    schema: {
      params: { artistId: { type: 'integer' } }
    },
    handler
  })

  function validateRoutePlugin (instance, opts, done) {
    const validateParams = function () {
      return instance.findRoute({
        method: 'GET',
        url: '/artists/:artistId'
      }) !== null
    }
    instance.decorate('validateRoutes', { validateParams })
    done()
  }

  fastify.register(fastifyPlugin(validateRoutePlugin))

Review comment (suggestion):
```suggestion
  t.assert.strictEqual(route.params.artistId, ':artistId')
```

Code after:
  })

  t.assert.strictEqual(fastify.findRoute({
    method: 'GET',
    url: '/books/:bookId'
  }), null)
})

test('findRoute should return the route when found', t => {
  t.plan(1)
  const fastify = Fastify()

  const handler = (req, reply) => reply.send(typeof req.params.artistId)

  fastify.get('/artists/:artistId', {
    schema: {
      params: { artistId: { type: 'integer' } }
    },
    handler
  })

  const route = fastify.findRoute({
    method: 'GET',
    url: '/artists/:artistId'
  })
  t.assert.strictEqual(route.params.artistId, ':artistId')
})

test('findRoute should work correctly when used within plugins', (t, done) => {
  t.plan(1)
  const fastify = Fastify()
  const handler = (req, reply) => reply.send(typeof req.params.artistId)
  fastify.get('/artists/:artistId', {
    schema: {
      params: { artistId: { type: 'integer' } }
    },
    handler
  })

  function validateRoutePlugin (instance, opts, done) {
    const validateParams = function () {
      return instance.findRoute({
        method: 'GET',
        url: '/artists/:artistId'
      }) !== null
    }
    instance.decorate('validateRoutes', { validateParams })
    done()
  }

  fastify.register(fastifyPlugin(validateRoutePlugin))
Repository: fastify/fastify
File: test/nullable-validation.test.js (JavaScript)

Code before:
'use strict'

const { test } = require('node:test')
const sget = require('simple-get').concat
const Fastify = require('..')

test('nullable string', (t, done) => {
  t.plan(3)
  const fastify = Fastify()
  fastify.route({
    method: 'POST',
    url: '/',
    handler: (req, reply) => {
      t.assert.deepStrictEqual(req.body.hello, null)
      reply.code(200).send(req.body)
    },
    schema: {
      body: {
        type: 'object',
        properties: {
          hello: {
            type: 'string',
            format: 'email',
            nullable: true
          }
        }
      },
      response: {
        200: {
          type: 'object',
          properties: {
            hello: {
              type: 'string',
              format: 'email',
              nullable: true
            }
          }
        }
      }

Review comment (suggestion):
Since we are testing the hello value in the object i think we can use `strictEqual`
```suggestion
      t.assert.strictEqual(req.body.hello, null)
```

Code after:
'use strict'

const { test } = require('node:test')
const sget = require('simple-get').concat
const Fastify = require('..')

test('nullable string', (t, done) => {
  t.plan(3)
  const fastify = Fastify()
  fastify.route({
    method: 'POST',
    url: '/',
    handler: (req, reply) => {
      t.assert.strictEqual(req.body.hello, null)
      reply.code(200).send(req.body)
    },
    schema: {
      body: {
        type: 'object',
        properties: {
          hello: {
            type: 'string',
            format: 'email',
            nullable: true
          }
        }
      },
      response: {
        200: {
          type: 'object',
          properties: {
            hello: {
              type: 'string',
              format: 'email',
              nullable: true
            }
          }
        }
      }
Repository: fastify/fastify
File: test/nullable-validation.test.js (JavaScript)

Code before:
          }
        }
      },
      response: {
        200: {
          type: 'object',
          properties: {
            hello: {
              type: 'string',
              format: 'email',
              nullable: true
            }
          }
        }
      }
    }
  })
  fastify.inject({
    method: 'POST',
    url: '/',
    body: {
      hello: undefined
    }
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.deepStrictEqual(res.payload.hello, undefined)
    done()
  })
})

test('object or null body', (t, done) => {
  t.plan(5)

  const fastify = Fastify()

  fastify.route({
    method: 'POST',
    url: '/',
    handler: (req, reply) => {
      t.assert.strictEqual(req.body, undefined)
      reply.code(200).send({ isUndefinedBody: req.body === undefined })
    },
    schema: {
      body: {
        type: ['object', 'null'],
        properties: {
          hello: {
            type: 'string',
            format: 'email'
          }
        }

Review comment (suggestion):
same here
```suggestion
    t.assert.strictEqual(res.payload.hello, undefined)
```

Code after:
          }
        }
      },
      response: {
        200: {
          type: 'object',
          properties: {
            hello: {
              type: 'string',
              format: 'email',
              nullable: true
            }
          }
        }
      }
    }
  })
  fastify.inject({
    method: 'POST',
    url: '/',
    body: {
      hello: null
    }
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.json().hello, null)
    done()
  })
})

test('object or null body', (t, done) => {
  t.plan(5)

  const fastify = Fastify()

  fastify.route({
    method: 'POST',
    url: '/',
    handler: (req, reply) => {
      t.assert.strictEqual(req.body, undefined)
      reply.code(200).send({ isUndefinedBody: req.body === undefined })
    },
    schema: {
      body: {
        type: ['object', 'null'],
        properties: {
          hello: {
            type: 'string',
            format: 'email'
          }
        }
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:
  fastify.post('/', (request, reply) => {
    reply.send(request.body)
  })

  fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: '{"hello":"world"}',
      headers: {
        'Content-Type': 'text/json'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(JSON.parse(body.toString()), { hello: 'world' })
      done()
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)

    t.test('in POST', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({

Review comment (suggestion):
Need to make this async / await to await for the child tests
```suggestion
test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  t.plan(3)
```

Code after:
  fastify.post('/', (request, reply) => {
    reply.send(request.body)
  })

  fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: '{"hello":"world"}',
      headers: {
        'Content-Type': 'text/json'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(JSON.parse(body.toString()), { hello: 'world' })
      done()
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)

    t.test('in POST', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })

    t.test('in OPTIONS', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',

Review comment (suggestion):
```suggestion
    await t.test('in POST', (t) => {
```

Code after:
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })

    await t.test('in OPTIONS', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)

    t.test('in POST', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })

    t.test('in OPTIONS', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'weird/content-type+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })
  })
})

test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
  t.plan(6)
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {

Review comment (suggestion):
```suggestion
    await t.test('in OPTIONS', (t) => {
```

Code after:
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })

    await t.test('in OPTIONS', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'weird/content-type+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })
  })
})

test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
  t.plan(6)
  const fastify = Fastify()
  t.after(() => fastify.close())
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })

    await t.test('in OPTIONS', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',

Review comment (suggestion):
we would still need to add in `done` and call after last assert to ensure the planned asserts were run
```suggestion
    await t.test('in POST', (t, done) => {
```

Code after:
    })
  })
})

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })

    await t.test('in OPTIONS', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })

    await t.test('in OPTIONS', (t) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'weird/content-type+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
      })
    })
  })
})

test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
  t.plan(6)
  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.post('/', (req, reply) => {

Review comment (suggestion):
we would still need to add in `done` and call after last assert
```suggestion
    await t.test('in POST', (t, done) => {
```

Code after:
      done(err, body)
    })
  })

  fastify.listen({ port: 0 }, async err => {
    t.assert.ifError(err)

    await t.test('in POST', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'POST',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'application/vnd.test+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })

    await t.test('in OPTIONS', (t, done) => {
      t.plan(3)
      t.after(() => fastify.close())

      sget({
        method: 'OPTIONS',
        url: getServerUrl(fastify),
        body: '{"hello":"world"}',
        headers: {
          'Content-Type': 'weird/content-type+json'
        }
      }, (err, response, body) => {
        t.assert.ifError(err)
        t.assert.strictEqual(response.statusCode, 200)
        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
        done()
      })
    })
  })
})

test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
  t.plan(6)
  const fastify = Fastify()
  t.after(() => fastify.close())
Repository: fastify/fastify
File: test/custom-parser.3.test.js (JavaScript)

Code before:

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: 'body',
      headers: {
        'Content-Type': 'very-weird-content-type'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(body.toString(), 'body')
    })

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: 'my text',
      headers: {
        'Content-Type': 'text/html'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(body.toString(), 'my texthtml')
      done()
    })
  })
})


Review comment (suggestion):
When we run multiple calls in parallel (no await), we must wait for them all to be completed before calling `done`.

In this scenario, the last sget may end before the other 2, and the test would complete anyway.

Example solution: https://github.com/fastify/fastify/pull/5881/files#diff-6a98dbdd7ca9fb8e0adfcc8787c950f65344cd9581437118754f4adb1a791f7d


Code after:
      headers: {
        'Content-Type': 'application/json'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(body.toString(), JSON.stringify({ myKey: 'myValue' }))
      completed()
    })

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: 'body',
      headers: {
        'Content-Type': 'very-weird-content-type'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(body.toString(), 'body')
      completed()
    })

    sget({
      method: 'POST',
      url: getServerUrl(fastify),
      body: 'my text',
      headers: {
        'Content-Type': 'text/html'
      }
    }, (err, response, body) => {
      t.assert.ifError(err)
      t.assert.strictEqual(response.statusCode, 200)
      t.assert.deepStrictEqual(body.toString(), 'my texthtml')
      completed()
    })
  })
})