From a8353251dc0d17fcf4e2bcd3ccaad755e5a83109 Mon Sep 17 00:00:00 2001 From: Antonio Tripodi Date: Sun, 22 Dec 2024 10:32:10 +0100 Subject: [PATCH] test: migrated custom-parser.3.test.js from tap to node:test (#5903) --- test/custom-parser.3.test.js | 101 +++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/test/custom-parser.3.test.js b/test/custom-parser.3.test.js index ec8ac2b03d..b81110a253 100644 --- a/test/custom-parser.3.test.js +++ b/test/custom-parser.3.test.js @@ -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('../fastify') +const Fastify = require('..') const jsonParser = require('fast-json-body') const { getServerUrl } = require('./helper') process.removeAllListeners('warning') -test('should be able to use default parser for extra content type', t => { +test('should be able to use default parser for extra content type', (t, done) => { t.plan(4) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) fastify.post('/', (request, reply) => { reply.send(request.body) @@ -21,7 +20,7 @@ test('should be able to use default parser for extra content type', t => { fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore')) fastify.listen({ port: 0 }, err => { - t.error(err) + t.assert.ifError(err) sget({ method: 'POST', @@ -31,18 +30,17 @@ test('should be able to use default parser for extra content type', t => { 'Content-Type': 'text/json' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.strictSame(JSON.parse(body.toString()), { hello: 'world' }) - fastify.close() + 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 => { - t.plan(3) - +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) @@ -58,13 +56,12 @@ test('contentTypeParser should add a custom parser with RegExp value', t => { }) }) - fastify.listen({ port: 0 }, err => { - t.error(err) - - t.teardown(() => fastify.close()) + fastify.listen({ port: 0 }, async err => { + t.assert.ifError(err) - t.test('in POST', t => { + await t.test('in POST', (t, done) => { t.plan(3) + t.after(() => fastify.close()) sget({ method: 'POST', @@ -74,14 +71,16 @@ test('contentTypeParser should add a custom parser with RegExp value', t => { 'Content-Type': 'application/vnd.test+json' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(body.toString(), JSON.stringify({ hello: 'world' })) + t.assert.ifError(err) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' })) + done() }) }) - t.test('in OPTIONS', t => { + await t.test('in OPTIONS', (t, done) => { t.plan(3) + t.after(() => fastify.close()) sget({ method: 'OPTIONS', @@ -91,9 +90,10 @@ test('contentTypeParser should add a custom parser with RegExp value', t => { 'Content-Type': 'weird/content-type+json' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(body.toString(), JSON.stringify({ hello: 'world' })) + t.assert.ifError(err) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' })) + done() }) }) }) @@ -102,7 +102,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => { test('contentTypeParser should add multiple custom parsers with RegExp values', async t => { t.plan(6) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) fastify.post('/', (req, reply) => { reply.send(req.body) @@ -137,8 +137,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values', 'Content-Type': 'application/vnd.hello+json' } }) - t.equal(response.statusCode, 200) - t.same(response.payload.toString(), '{"hello":"world"}') + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}') } { @@ -150,8 +150,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values', 'Content-Type': 'application/test+xml' } }) - t.equal(response.statusCode, 200) - t.same(response.payload.toString(), 'xml') + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(response.payload.toString(), 'xml') } await fastify.inject({ @@ -162,17 +162,17 @@ test('contentTypeParser should add multiple custom parsers with RegExp values', 'Content-Type': 'application/+myExtension' } }).then((response) => { - t.equal(response.statusCode, 200) - t.same(response.payload.toString(), 'abcdefgmyExtension') + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(response.payload.toString(), 'abcdefgmyExtension') }).catch((err) => { - t.error(err) + t.assert.ifError(err) }) }) -test('catch all content type parser should not interfere with content type parser', t => { +test('catch all content type parser should not interfere with content type parser', (t, done) => { t.plan(10) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) fastify.post('/', (req, reply) => { reply.send(req.body) @@ -201,7 +201,15 @@ test('catch all content type parser should not interfere with content type parse }) fastify.listen({ port: 0 }, err => { - t.error(err) + t.assert.ifError(err) + + let pending = 3 + + function completed () { + if (--pending === 0) { + done() + } + } sget({ method: 'POST', @@ -211,9 +219,10 @@ test('catch all content type parser should not interfere with content type parse 'Content-Type': 'application/json' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(body.toString(), JSON.stringify({ myKey: 'myValue' })) + t.assert.ifError(err) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(body.toString(), JSON.stringify({ myKey: 'myValue' })) + completed() }) sget({ @@ -224,9 +233,10 @@ test('catch all content type parser should not interfere with content type parse 'Content-Type': 'very-weird-content-type' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(body.toString(), 'body') + t.assert.ifError(err) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(body.toString(), 'body') + completed() }) sget({ @@ -237,9 +247,10 @@ test('catch all content type parser should not interfere with content type parse 'Content-Type': 'text/html' } }, (err, response, body) => { - t.error(err) - t.equal(response.statusCode, 200) - t.same(body.toString(), 'my texthtml') + t.assert.ifError(err) + t.assert.strictEqual(response.statusCode, 200) + t.assert.deepStrictEqual(body.toString(), 'my texthtml') + completed() }) }) })