Skip to content

Commit

Permalink
test: migrated nullable-validation.test.js from tap to node:test (fas…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony133 authored Dec 21, 2024
1 parent fa95e84 commit 221ec45
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions test/nullable-validation.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

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

test('nullable string', t => {
test('nullable string', (t, done) => {
t.plan(3)
const fastify = Fastify()
fastify.route({
method: 'POST',
url: '/',
handler: (req, reply) => {
t.same(req.body.hello, null)
t.assert.strictEqual(req.body.hello, null)
reply.code(200).send(req.body)
},
schema: {
Expand Down Expand Up @@ -47,12 +46,13 @@ test('nullable string', t => {
hello: null
}
}, (err, res) => {
t.error(err)
t.same(res.payload.hello, null)
t.assert.ifError(err)
t.assert.strictEqual(res.json().hello, null)
done()
})
})

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

const fastify = Fastify()
Expand All @@ -61,7 +61,7 @@ test('object or null body', t => {
method: 'POST',
url: '/',
handler: (req, reply) => {
t.equal(req.body, undefined)
t.assert.strictEqual(req.body, undefined)
reply.code(200).send({ isUndefinedBody: req.body === undefined })
},
schema: {
Expand Down Expand Up @@ -89,21 +89,22 @@ test('object or null body', t => {
})

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

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { isUndefinedBody: true })
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
done()
})
})
})

test('nullable body', t => {
test('nullable body', (t, done) => {
t.plan(5)

const fastify = Fastify()
Expand All @@ -112,7 +113,7 @@ test('nullable body', t => {
method: 'POST',
url: '/',
handler: (req, reply) => {
t.equal(req.body, undefined)
t.assert.strictEqual(req.body, undefined)
reply.code(200).send({ isUndefinedBody: req.body === undefined })
},
schema: {
Expand Down Expand Up @@ -141,21 +142,22 @@ test('nullable body', t => {
})

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

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { isUndefinedBody: true })
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
done()
})
})
})

test('Nullable body with 204', t => {
test('Nullable body with 204', (t, done) => {
t.plan(5)

const fastify = Fastify()
Expand All @@ -164,7 +166,7 @@ test('Nullable body with 204', t => {
method: 'POST',
url: '/',
handler: (req, reply) => {
t.equal(req.body, undefined)
t.assert.strictEqual(req.body, undefined)
reply.code(204).send()
},
schema: {
Expand All @@ -182,16 +184,17 @@ test('Nullable body with 204', t => {
})

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

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 204)
t.equal(body.length, 0)
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 204)
t.assert.strictEqual(body.length, 0)
done()
})
})
})

0 comments on commit 221ec45

Please sign in to comment.