From 1330ba04a1bdbd60de07f14dbe35fa2c05333cb0 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 7 Feb 2025 22:36:24 +0100 Subject: [PATCH] feat: migrate to node test runner --- package.json | 4 +-- test/duplicate-schema.test.js | 6 ++--- test/plugin.test.js | 12 ++++----- test/standalone.test.js | 46 +++++++++++++++++------------------ 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index c409304..6dd335d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "lint": "eslint", "lint:fix": "eslint --fix", - "unit": "tap test/**/*.test.js", + "unit": "c8 --100 node --test", "test": "npm run unit && npm run test:typescript", "test:typescript": "tsd" }, @@ -54,11 +54,11 @@ ], "devDependencies": { "@fastify/pre-commit": "^2.1.0", + "c8": "^10.1.3", "eslint": "^9.17.0", "fastify": "^5.0.0", "neostandard": "^0.12.0", "sanitize-filename": "^1.6.3", - "tap": "^18.7.2", "tsd": "^0.31.0" }, "pre-commit": [ diff --git a/test/duplicate-schema.test.js b/test/duplicate-schema.test.js index d6904ed..2acaa24 100644 --- a/test/duplicate-schema.test.js +++ b/test/duplicate-schema.test.js @@ -1,9 +1,9 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const FjsCompiler = require('../index') -t.test('Use input schema duplicate in the externalSchemas', async t => { +test('Use input schema duplicate in the externalSchemas', async t => { t.plan(1) const externalSchemas = { schema1: { @@ -22,5 +22,5 @@ t.test('Use input schema duplicate in the externalSchemas', async t => { compiler({ schema: externalSchemas.schema1 }) compiler({ schema: externalSchemas.schema2 }) - t.pass() + t.assert.ok(true) }) diff --git a/test/plugin.test.js b/test/plugin.test.js index 11e34d8..26ca0f5 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -1,6 +1,6 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const fastify = require('fastify') const FjsCompiler = require('../index') @@ -27,16 +27,16 @@ const externalSchemas2 = Object.freeze({ const fastifyFjsOptionsDefault = Object.freeze({}) -t.test('basic usage', t => { +test('basic usage', t => { t.plan(1) const factory = FjsCompiler() const compiler = factory(externalSchemas1, fastifyFjsOptionsDefault) const serializeFunc = compiler({ schema: sampleSchema }) const result = serializeFunc({ name: 'hello' }) - t.equal(result, '{"name":"hello"}') + t.assert.equal(result, '{"name":"hello"}') }) -t.test('fastify integration', async t => { +test('fastify integration', async t => { const factory = FjsCompiler() const app = fastify({ @@ -73,6 +73,6 @@ t.test('fastify integration', async t => { } }) - t.equal(res.statusCode, 200) - t.same(res.json(), { name: 'serialize me' }) + t.assert.equal(res.statusCode, 200) + t.assert.deepStrictEqual(res.json(), { name: 'serialize me' }) }) diff --git a/test/standalone.test.js b/test/standalone.test.js index ba11330..6ecdc17 100644 --- a/test/standalone.test.js +++ b/test/standalone.test.js @@ -2,7 +2,7 @@ const fs = require('node:fs') const path = require('node:path') -const t = require('tap') +const { test } = require('node:test') const fastify = require('fastify') const sanitize = require('sanitize-filename') @@ -16,10 +16,10 @@ function generateFileName (routeOpts) { return fileName } -t.test('standalone', t => { +test('standalone', async t => { t.plan(5) - t.teardown(async () => { + t.after(async () => { for (const fileName of generatedFileNames) { try { await fs.promises.unlink(path.join(__dirname, fileName)) @@ -29,10 +29,10 @@ t.test('standalone', t => { t.test('errors', t => { t.plan(2) - t.throws(() => { + t.assert.throws(() => { FjsStandaloneCompiler() }, 'missing restoreFunction') - t.throws(() => { + t.assert.throws(() => { FjsStandaloneCompiler({ readMode: false }) }, 'missing storeFunction') }) @@ -74,28 +74,28 @@ t.test('standalone', t => { const factory = FjsStandaloneCompiler({ readMode: false, storeFunction (routeOpts, schemaSerializerCode) { - t.same(routeOpts, endpointSchema) - t.type(schemaSerializerCode, 'string') + t.assert.deepStrictEqual(routeOpts, endpointSchema) + t.assert.ok(typeof schemaSerializerCode === 'string') fs.writeFileSync(path.join(__dirname, '/fjs-generated.js'), schemaSerializerCode) generatedFileNames.push('/fjs-generated.js') - t.pass('stored the serializer function') + t.assert.ok('stored the serializer function') } }) const compiler = factory(schemaMap) compiler(endpointSchema) - t.pass('compiled the endpoint schema') + t.assert.ok('compiled the endpoint schema') t.test('usage standalone code', t => { t.plan(3) const standaloneSerializer = require('./fjs-generated') - t.ok(standaloneSerializer) + t.assert.ok(standaloneSerializer) const valid = standaloneSerializer({ hello: 'world' }) - t.same(valid, JSON.stringify({ hello: 'world' })) + t.assert.deepStrictEqual(valid, JSON.stringify({ hello: 'world' })) const invalid = standaloneSerializer({ hello: [] }) - t.same(invalid, '{"hello":""}') + t.assert.deepStrictEqual(invalid, '{"hello":""}') }) }) @@ -106,9 +106,9 @@ t.test('standalone', t => { readMode: false, storeFunction (routeOpts, schemaSerializationCode) { const fileName = generateFileName(routeOpts) - t.ok(routeOpts) + t.assert.ok(routeOpts) fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode) - t.pass(`stored the serializer function ${fileName}`) + t.assert.ok(`stored the serializer function ${fileName}`) }, restoreFunction () { t.fail('write mode ON') @@ -119,16 +119,16 @@ t.test('standalone', t => { await app.ready() }) - t.test('fastify integration - writeMode forces standalone', async t => { + await t.test('fastify integration - writeMode forces standalone', async t => { t.plan(4) const factory = FjsStandaloneCompiler({ readMode: false, storeFunction (routeOpts, schemaSerializationCode) { const fileName = generateFileName(routeOpts) - t.ok(routeOpts) + t.assert.ok(routeOpts) fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode) - t.pass(`stored the serializer function ${fileName}`) + t.assert.ok(`stored the serializer function ${fileName}`) }, restoreFunction () { t.fail('write mode ON') @@ -143,7 +143,7 @@ t.test('standalone', t => { await app.ready() }) - t.test('fastify integration - readMode', async t => { + await t.test('fastify integration - readMode', async t => { t.plan(6) const factory = FjsStandaloneCompiler({ @@ -153,7 +153,7 @@ t.test('standalone', t => { }, restoreFunction (routeOpts) { const fileName = generateFileName(routeOpts) - t.pass(`restore the serializer function ${fileName}}`) + t.assert.ok(`restore the serializer function ${fileName}}`) return require(path.join(__dirname, fileName)) } }) @@ -165,15 +165,15 @@ t.test('standalone', t => { url: '/foo', method: 'POST' }) - t.equal(res.statusCode, 200) - t.equal(res.payload, JSON.stringify({ hello: 'world' })) + t.assert.equal(res.statusCode, 200) + t.assert.equal(res.payload, JSON.stringify({ hello: 'world' })) res = await app.inject({ url: '/bar?lang=it', method: 'GET' }) - t.equal(res.statusCode, 200) - t.equal(res.payload, JSON.stringify({ lang: 'en' })) + t.assert.equal(res.statusCode, 200) + t.assert.equal(res.payload, JSON.stringify({ lang: 'en' })) }) function buildApp (factory, serializerOpts) {