From 29fd7d988565b0992f1e48da3929be38e6256c0d Mon Sep 17 00:00:00 2001 From: Simone Busoli Date: Tue, 12 Nov 2024 13:12:16 +0100 Subject: [PATCH] chore: node test migration and ts fix --- .gitignore | 3 --- package.json | 5 ++--- test/fastify.test.js | 42 ++++++++++++++-------------------- test/formatDate.test.js | 4 ++-- test/index.test.js | 50 ++++++++++++++++------------------------- types/index.test-d.ts | 4 ++-- 6 files changed, 42 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 2b6aed4..3c21249 100644 --- a/.gitignore +++ b/.gitignore @@ -147,6 +147,3 @@ yarn.lock # editor files .vscode .idea - -#tap files -.tap/ diff --git a/package.json b/package.json index 657bbaf..1b7db47 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "fastify": "^5.0.0", "snazzy": "^9.0.0", "standard": "^17.1.0", - "tap": "^18.7.2", "tsd": "^0.31.0" }, "resolutions": { @@ -29,8 +28,8 @@ "lint:fix": "standard --fix", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", - "test:unit": "tap", - "test:watch": "tap --watch --no-coverage-report" + "test:unit": "node --test --experimental-test-coverage", + "test:watch": "node --test --watch" }, "repository": { "type": "git", diff --git a/test/fastify.test.js b/test/fastify.test.js index e32da61..e06174b 100644 --- a/test/fastify.test.js +++ b/test/fastify.test.js @@ -2,22 +2,20 @@ const { serverFactory, TIME, unmockTime, mockTime } = require('./helpers') const pretty = require('pino-pretty') -const tap = require('tap') - -const { test } = tap +const { before, beforeEach, after, test } = require('node:test') const messages = [] let server = serverFactory(messages, { colorize: false }) -tap.before(() => { +before(() => { mockTime() }) -tap.teardown(() => { +after(() => { unmockTime() }) -tap.beforeEach(() => { +beforeEach(() => { // empty messages array messages.splice(0, messages.length) @@ -27,21 +25,20 @@ tap.beforeEach(() => { test('should log server started messages', async (t) => { t.beforeEach(async () => { await server.listen({ port: 63995 }) - t.teardown(async () => await server.close()) + t.afterEach(async () => await server.close()) }) - t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { + await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const messagesExpected = [ `${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n`, `${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://[::1]:63995\x1B[39m\n` ] // sort because the order of the messages is not guaranteed - t.same(messages.sort(), messagesExpected.sort()) - t.end() + t.assert.strictEqual(messages.sort(), messagesExpected.sort()) }) - t.test( + await t.test( 'colors not supported in TTY', { skip: pretty.isColorSupported }, (t) => { @@ -51,8 +48,7 @@ test('should log server started messages', async (t) => { ] // sort because the order of the messages is not guaranteed - t.same(messages.sort(), messagesExpected.sort()) - t.end() + t.assert.strictEqual(messages.sort(), messagesExpected.sort()) } ) }) @@ -72,7 +68,7 @@ methods.forEach((method) => { }) }) - t.test( + await t.test( 'colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { @@ -80,12 +76,11 @@ methods.forEach((method) => { `${TIME} - \x1B[32minfo\x1B[39m - ${method} /path - \x1B[36mincoming request\x1B[39m\n`, `${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mrequest completed\x1B[39m\n` ] - t.same(messages, messagesExpected) - t.end() + t.assert.deepStrictEqual(messages, messagesExpected) } ) - t.test( + await t.test( 'colors not supported in TTY', { skip: pretty.isColorSupported }, (t) => { @@ -93,8 +88,7 @@ methods.forEach((method) => { `${TIME} - info - ${method} /path - incoming request\n`, `${TIME} - info - request completed\n` ] - t.same(messages, messagesExpected) - t.end() + t.assert.deepStrictEqual(messages, messagesExpected) } ) }) @@ -118,7 +112,7 @@ test('should handle user defined log', async (t) => { await server.inject('/a-path-with-user-defined-log') }) - t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { + await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const messagesExpected = [ `${TIME} - \x1B[32minfo\x1B[39m - GET /a-path-with-user-defined-log - \x1B[36mincoming request\x1B[39m\n`, `${TIME} - \x1B[41mfatal\x1B[49m - \x1B[36ma user defined fatal log\x1B[39m\n`, @@ -129,11 +123,10 @@ test('should handle user defined log', async (t) => { `${TIME} - \x1B[90mtrace\x1B[39m - \x1B[36ma user defined trace log\x1B[39m\n`, `${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mrequest completed\x1B[39m\n` ] - t.same(messages, messagesExpected) - t.end() + t.assert.deepStrictEqual(messages, messagesExpected) }) - t.test( + await t.test( 'colors not supported in TTY', { skip: pretty.isColorSupported }, (t) => { @@ -147,8 +140,7 @@ test('should handle user defined log', async (t) => { `${TIME} - trace - a user defined trace log\n`, `${TIME} - info - request completed\n` ] - t.same(messages, messagesExpected) - t.end() + t.assert.deepStrictEqual(messages, messagesExpected) } ) }) diff --git a/test/formatDate.test.js b/test/formatDate.test.js index 1b435ed..3ac424a 100644 --- a/test/formatDate.test.js +++ b/test/formatDate.test.js @@ -1,7 +1,7 @@ 'use strict' const formatDate = require('../lib/formatDate') -const test = require('tap').test +const { test } = require('node:test') const timeFormatRE = /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d$/ @@ -12,6 +12,6 @@ test('should generate valid formatted time', (t) => { for (let i = 0; i < iterations; ++i) { const randomInt = Math.floor(Math.random() * maxTimestamp) - t.ok(timeFormatRE.test(formatDate(randomInt))) + t.assert.ok(timeFormatRE.test(formatDate(randomInt))) } }) diff --git a/test/index.test.js b/test/index.test.js index f5732a0..c985072 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -2,30 +2,26 @@ const { EPOCH, TIME, MESSAGE_KEY, mockTime, unmockTime } = require('./helpers') const target = require('..') -const tap = require('tap') +const { before, after, test } = require('node:test') const pretty = require('pino-pretty') const { messageFormatFactory } = target -const { test } = tap - const messageFormat = messageFormatFactory( undefined, undefined, pretty.isColorSupported ) -tap.before(() => { +before(() => { mockTime() }) -tap.teardown(() => { +after(() => { unmockTime() }) test('able to instantiate target without arguments', (t) => { target() - t.pass() - t.end() }) test('format log correctly with different logDescriptor', async (t) => { @@ -50,15 +46,14 @@ test('format log correctly with different logDescriptor', async (t) => { ] ] - await logDescriptorLogPairs.forEach( + await Promise.all(logDescriptorLogPairs.map( async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => { await t.test( 'colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogColored) - t.end() + t.assert.strictEqual(log, expectedLogColored) } ) @@ -67,12 +62,11 @@ test('format log correctly with different logDescriptor', async (t) => { { skip: pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogUncolored) - t.end() + t.assert.strictEqual(log, expectedLogUncolored) } ) } - ) + )) }) test('colorize log correctly with different logDescriptor', async (t) => { @@ -97,15 +91,14 @@ test('colorize log correctly with different logDescriptor', async (t) => { ] ] - await logDescriptorColorizedLogPairs.forEach( + await Promise.all(logDescriptorColorizedLogPairs.map( async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => { await t.test( 'colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogColored) - t.end() + t.assert.strictEqual(log, expectedLogColored) } ) @@ -114,12 +107,11 @@ test('colorize log correctly with different logDescriptor', async (t) => { { skip: pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogUncolored) - t.end() + t.assert.strictEqual(log, expectedLogUncolored) } ) } - ) + )) }) test('format log correctly with custom levels', async (t) => { @@ -154,15 +146,14 @@ test('format log correctly with custom levels', async (t) => { ] ] - await logCustomLevelsLogPairs.forEach( + await Promise.all(logCustomLevelsLogPairs.map( async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => { await t.test( 'colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogColored) - t.end() + t.assert.strictEqual(log, expectedLogColored) } ) @@ -171,12 +162,11 @@ test('format log correctly with custom levels', async (t) => { { skip: pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogUncolored) - t.end() + t.assert.strictEqual(log, expectedLogUncolored) } ) } - ) + )) }) test('format log correctly with custom colors per level', async (t) => { @@ -214,15 +204,14 @@ test('format log correctly with custom colors per level', async (t) => { ] ] - await logCustomLevelsLogPairs.forEach( + await Promise.all(logCustomLevelsLogPairs.map( async ([logDescriptor, expectedLogColored, expectedLogUncolored]) => { await t.test( 'colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogColored) - t.end() + t.assert.strictEqual(log, expectedLogColored) } ) @@ -231,10 +220,9 @@ test('format log correctly with custom colors per level', async (t) => { { skip: pretty.isColorSupported }, (t) => { const log = messageFormat(logDescriptor, MESSAGE_KEY) - t.equal(log, expectedLogUncolored) - t.end() + t.assert.strictEqual(log, expectedLogUncolored) } ) } - ) + )) }) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index a584afb..364857d 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,5 +1,5 @@ import pretty from 'pino-pretty'; -import { expectType } from "tsd"; +import { expectType, expectAssignable } from "tsd"; import oneLineLogger, { CustomColor, LogDescriptor, @@ -18,4 +18,4 @@ expectType(({} as LogDescriptor).req) expectType<(colorize: boolean, levels: Record, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory) expectType(oneLineLoggerNamed) -expectType<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger) +expectAssignable<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger)