diff --git a/.taprc b/.taprc deleted file mode 100644 index 3a5e97b..0000000 --- a/.taprc +++ /dev/null @@ -1,4 +0,0 @@ -files: - - test/**/*.js -plugin: - - "!@tapjs/typescript" diff --git a/package.json b/package.json index 926818a..d168f15 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "scripts": { "lint": "standard", "test": "npm run test:unit && npm run test:typescript", - "test:unit": "tap", + "test:unit": "c8 --100 node --test", + "test:coverage": "c8 node --test && c8 report --reporter=html", "test:typescript": "tsd" }, "repository": { @@ -30,10 +31,10 @@ "@fastify/pre-commit": "^2.1.0", "@fastify/type-provider-typebox": "^5.0.0-pre.fv5.1", "@types/node": "^22.0.0", + "c8": "^10.1.2", "fastify": "^5.0.0-alpha.2", "proxyquire": "^2.1.3", "standard": "^17.1.0", - "tap": "^18.7.0", "tsd": "^0.31.0" } } diff --git a/test/bundlers.test.js b/test/bundlers.test.js index b04a3c3..ffda456 100644 --- a/test/bundlers.test.js +++ b/test/bundlers.test.js @@ -1,12 +1,12 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const fp = require('../plugin') -test('webpack removes require.main.filename', (t) => { +test('webpack removes require.main.filename', t => { const filename = require.main.filename const info = console.info - t.teardown(() => { + t.after(() => { require.main.filename = filename console.info = info }) @@ -14,7 +14,7 @@ test('webpack removes require.main.filename', (t) => { require.main.filename = null console.info = function (msg) { - t.fail('logged: ' + msg) + t.assert.fail('logged: ' + msg) } fp((fastify, opts, next) => { @@ -22,8 +22,6 @@ test('webpack removes require.main.filename', (t) => { }, { fastify: '^5.0.0' }) - - t.end() }) test('support faux modules', (t) => { @@ -31,8 +29,7 @@ test('support faux modules', (t) => { next() }) - t.equal(plugin.default, plugin) - t.end() + t.assert.strictEqual(plugin.default, plugin) }) test('support faux modules does not override existing default field in babel module', (t) => { @@ -44,8 +41,7 @@ test('support faux modules does not override existing default field in babel mod const plugin = fp(module) - t.equal(plugin.default, 'Existing default field') - t.end() + t.assert.strictEqual(plugin.default, 'Existing default field') }) test('support ts named imports', (t) => { @@ -55,8 +51,7 @@ test('support ts named imports', (t) => { name: 'hello' }) - t.equal(plugin.hello, plugin) - t.end() + t.assert.strictEqual(plugin.hello, plugin) }) test('from kebab-case to camelCase', (t) => { @@ -66,8 +61,7 @@ test('from kebab-case to camelCase', (t) => { name: 'hello-world' }) - t.equal(plugin.helloWorld, plugin) - t.end() + t.assert.strictEqual(plugin.helloWorld, plugin) }) test('from @-prefixed named imports', (t) => { @@ -77,8 +71,7 @@ test('from @-prefixed named imports', (t) => { name: '@hello/world' }) - t.equal(plugin.helloWorld, plugin) - t.end() + t.assert.strictEqual(plugin.helloWorld, plugin) }) test('from @-prefixed named kebab-case to camelCase', (t) => { @@ -88,8 +81,7 @@ test('from @-prefixed named kebab-case to camelCase', (t) => { name: '@hello/my-world' }) - t.equal(plugin.helloMyWorld, plugin) - t.end() + t.assert.strictEqual(plugin.helloMyWorld, plugin) }) test('from kebab-case to camelCase multiple words', (t) => { @@ -99,8 +91,7 @@ test('from kebab-case to camelCase multiple words', (t) => { name: 'hello-long-world' }) - t.equal(plugin.helloLongWorld, plugin) - t.end() + t.assert.strictEqual(plugin.helloLongWorld, plugin) }) test('from kebab-case to camelCase multiple words does not override', (t) => { @@ -115,6 +106,5 @@ test('from kebab-case to camelCase multiple words does not override', (t) => { name: 'hello-long-world' }) - t.equal(plugin.helloLongWorld, foobar) - t.end() + t.assert.strictEqual(plugin.helloLongWorld, foobar) }) diff --git a/test/checkVersion.test.js b/test/checkVersion.test.js index 72672d3..ece5b24 100644 --- a/test/checkVersion.test.js +++ b/test/checkVersion.test.js @@ -1,17 +1,17 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const fp = require('../plugin') test('checkVersion having require.main.filename', (t) => { const info = console.info - t.ok(require.main.filename) - t.teardown(() => { + t.assert.ok(require.main.filename) + t.after(() => { console.info = info }) console.info = function (msg) { - t.fail('logged: ' + msg) + t.assert.fail('logged: ' + msg) } fp((fastify, opts, next) => { @@ -19,14 +19,12 @@ test('checkVersion having require.main.filename', (t) => { }, { fastify: '^5.0.0' }) - - t.end() }) test('checkVersion having no require.main.filename but process.argv[1]', (t) => { const filename = require.main.filename const info = console.info - t.teardown(() => { + t.after(() => { require.main.filename = filename console.info = info }) @@ -34,7 +32,7 @@ test('checkVersion having no require.main.filename but process.argv[1]', (t) => require.main.filename = null console.info = function (msg) { - t.fail('logged: ' + msg) + t.assert.fail('logged: ' + msg) } fp((fastify, opts, next) => { @@ -42,15 +40,13 @@ test('checkVersion having no require.main.filename but process.argv[1]', (t) => }, { fastify: '^5.0.0' }) - - t.end() }) test('checkVersion having no require.main.filename and no process.argv[1]', (t) => { const filename = require.main.filename const argv = process.argv const info = console.info - t.teardown(() => { + t.after(() => { require.main.filename = filename process.argv = argv console.info = info @@ -60,7 +56,7 @@ test('checkVersion having no require.main.filename and no process.argv[1]', (t) process.argv[1] = null console.info = function (msg) { - t.fail('logged: ' + msg) + t.assert.fail('logged: ' + msg) } fp((fastify, opts, next) => { @@ -68,6 +64,4 @@ test('checkVersion having no require.main.filename and no process.argv[1]', (t) }, { fastify: '^5.0.0' }) - - t.end() }) diff --git a/test/composite.test.js b/test/composite.test.js index eca5a7e..714a1e0 100644 --- a/test/composite.test.js +++ b/test/composite.test.js @@ -1,16 +1,14 @@ 'use strict' -const t = require('tap') -const test = t.test +const { test } = require('node:test') const fp = require('../plugin') -test('anonymous function should be named composite.test0', t => { +test('anonymous function should be named composite.test0', (t) => { t.plan(2) - const fn = fp((fastify, opts, next) => { next() }) - t.equal(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0') - t.equal(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0') + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0') + t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0') }) diff --git a/test/esm/esm.mjs b/test/esm/esm.mjs index 38b6ed0..768a1af 100644 --- a/test/esm/esm.mjs +++ b/test/esm/esm.mjs @@ -1,13 +1,11 @@ -import t from 'tap' - +import { test } from 'node:test' import fp from '../../plugin.js' -t.test('esm base support', async t => { +test('esm base support', (t) => { fp((fastify, opts, next) => { next() }, { fastify: '^5.0.0' }) - - t.end() + t.assert.ok(true, 'fp function called without throwing an error') }) diff --git a/test/extractPluginName.test.js b/test/extractPluginName.test.js index e056c8a..23097e6 100644 --- a/test/extractPluginName.test.js +++ b/test/extractPluginName.test.js @@ -1,6 +1,6 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const extractPluginName = require('../lib/getPluginName').extractPluginName const winStack = `Error: anonymous function @@ -41,8 +41,9 @@ at TAP.test (/home/leonardo/desktop/fastify-plugin/node_modules/tap/lib/test.js: const anonymousStack = 'Unable to parse this' -t.plan(3) - -t.equal(extractPluginName(winStack), 'hello.test') -t.equal(extractPluginName(nixStack), 'this.is.a.test') -t.equal(extractPluginName(anonymousStack), 'anonymous') +test('extractPluginName tests', (t) => { + t.plan(3) + t.assert.strictEqual(extractPluginName(winStack), 'hello.test') + t.assert.strictEqual(extractPluginName(nixStack), 'this.is.a.test') + t.assert.strictEqual(extractPluginName(anonymousStack), 'anonymous') +}) diff --git a/test/mu1tip1e.composite.test.js b/test/mu1tip1e.composite.test.js index 7e3474d..5696c0b 100644 --- a/test/mu1tip1e.composite.test.js +++ b/test/mu1tip1e.composite.test.js @@ -1,16 +1,15 @@ 'use strict' -const t = require('tap') -const test = t.test +const { test } = require('node:test') const fp = require('../plugin') -test('anonymous function should be named mu1tip1e.composite.test', t => { +test('anonymous function should be named mu1tip1e.composite.test', (t) => { t.plan(2) const fn = fp((fastify, opts, next) => { next() }) - t.equal(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0') - t.equal(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0') + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0') + t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0') }) diff --git a/test/test.js b/test/test.js index ac52ee3..ff08e8f 100644 --- a/test/test.js +++ b/test/test.js @@ -1,19 +1,18 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const proxyquire = require('proxyquire') -const test = t.test const fp = require('../plugin') const Fastify = require('fastify') const pkg = require('../package.json') -test('fastify-plugin is a function', t => { +test('fastify-plugin is a function', (t) => { t.plan(1) - t.type(fp, 'function') + t.assert.ok(typeof fp === 'function') }) -test('should return the function with the skip-override Symbol', t => { +test('should return the function with the skip-override Symbol', (t) => { t.plan(1) function plugin (fastify, opts, next) { @@ -21,10 +20,10 @@ test('should return the function with the skip-override Symbol', t => { } fp(plugin) - t.ok(plugin[Symbol.for('skip-override')]) + t.assert.ok(plugin[Symbol.for('skip-override')]) }) -test('should support "default" function from babel module', t => { +test('should support "default" function from babel module', (t) => { t.plan(1) const plugin = { @@ -33,24 +32,24 @@ test('should support "default" function from babel module', t => { try { fp(plugin) - t.pass() + t.assert.ok(true) } catch (e) { - t.equal(e.message, 'fastify-plugin expects a function, instead got a \'object\'') + t.assert.strictEqual(e.message, 'fastify-plugin expects a function, instead got a \'object\'') } }) -test('should throw if the plugin is not a function', t => { +test('should throw if the plugin is not a function', (t) => { t.plan(1) try { fp('plugin') - t.fail() + t.assert.fail() } catch (e) { - t.equal(e.message, 'fastify-plugin expects a function, instead got a \'string\'') + t.assert.strictEqual(e.message, 'fastify-plugin expects a function, instead got a \'string\'') } }) -test('should check the fastify version', t => { +test('should check the fastify version', (t) => { t.plan(1) function plugin (fastify, opts, next) { @@ -59,13 +58,13 @@ test('should check the fastify version', t => { try { fp(plugin, { fastify: '>=0.10.0' }) - t.pass() + t.assert.ok(true) } catch (e) { - t.fail() + t.assert.fail() } }) -test('should check the fastify version', t => { +test('should check the fastify version', (t) => { t.plan(1) function plugin (fastify, opts, next) { @@ -74,42 +73,42 @@ test('should check the fastify version', t => { try { fp(plugin, '>=0.10.0') - t.pass() + t.assert.ok(true) } catch (e) { - t.fail() + t.assert.fail() } }) -test('the options object should be an object', t => { +test('the options object should be an object', (t) => { t.plan(2) try { fp(() => { }, null) - t.fail() + t.assert.fail() } catch (e) { - t.equal(e.message, 'The options object should be an object') + t.assert.strictEqual(e.message, 'The options object should be an object') } try { fp(() => { }, []) - t.fail() + t.assert.fail() } catch (e) { - t.equal(e.message, 'The options object should be an object') + t.assert.strictEqual(e.message, 'The options object should be an object') } }) -test('should throw if the version number is not a string', t => { +test('should throw if the version number is not a string', (t) => { t.plan(1) try { fp(() => { }, { fastify: 12 }) - t.fail() + t.assert.fail() } catch (e) { - t.equal(e.message, 'fastify-plugin expects a version string, instead got \'number\'') + t.assert.strictEqual(e.message, 'fastify-plugin expects a version string, instead got \'number\'') } }) -test('Should accept an option object', t => { +test('Should accept an option object', (t) => { t.plan(2) const opts = { hello: 'world' } @@ -119,11 +118,12 @@ test('Should accept an option object', t => { } fp(plugin, opts) - t.ok(plugin[Symbol.for('skip-override')]) - t.same(plugin[Symbol.for('plugin-meta')], opts) + + t.assert.ok(plugin[Symbol.for('skip-override')], 'skip-override symbol should be present') + t.assert.deepStrictEqual(plugin[Symbol.for('plugin-meta')], opts, 'plugin-meta should match opts') }) -test('Should accept an option object and checks the version', t => { +test('Should accept an option object and checks the version', (t) => { t.plan(2) const opts = { hello: 'world', fastify: '>=0.10.0' } @@ -133,31 +133,29 @@ test('Should accept an option object and checks the version', t => { } fp(plugin, opts) - t.ok(plugin[Symbol.for('skip-override')]) - t.same(plugin[Symbol.for('plugin-meta')], opts) + t.assert.ok(plugin[Symbol.for('skip-override')]) + t.assert.deepStrictEqual(plugin[Symbol.for('plugin-meta')], opts) }) -test('should set anonymous function name to file it was called from with a counter', t => { +test('should set anonymous function name to file it was called from with a counter', (t) => { const fp = proxyquire('../plugin.js', { stubs: {} }) const fn = fp((fastify, opts, next) => { next() }) - t.equal(fn[Symbol.for('plugin-meta')].name, 'test-auto-0') - t.equal(fn[Symbol.for('fastify.display-name')], 'test-auto-0') + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'test-auto-0') + t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'test-auto-0') const fn2 = fp((fastify, opts, next) => { next() }) - t.equal(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1') - t.equal(fn2[Symbol.for('fastify.display-name')], 'test-auto-1') - - t.end() + t.assert.strictEqual(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1') + t.assert.strictEqual(fn2[Symbol.for('fastify.display-name')], 'test-auto-1') }) -test('should set function name if Error.stackTraceLimit is set to 0', t => { +test('should set function name if Error.stackTraceLimit is set to 0', (t) => { const stackTraceLimit = Error.stackTraceLimit = 0 const fp = proxyquire('../plugin.js', { stubs: {} }) @@ -166,21 +164,20 @@ test('should set function name if Error.stackTraceLimit is set to 0', t => { next() }) - t.equal(fn[Symbol.for('plugin-meta')].name, 'test-auto-0') - t.equal(fn[Symbol.for('fastify.display-name')], 'test-auto-0') + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'test-auto-0') + t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'test-auto-0') const fn2 = fp((fastify, opts, next) => { next() }) - t.equal(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1') - t.equal(fn2[Symbol.for('fastify.display-name')], 'test-auto-1') + t.assert.strictEqual(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1') + t.assert.strictEqual(fn2[Symbol.for('fastify.display-name')], 'test-auto-1') Error.stackTraceLimit = stackTraceLimit - t.end() }) -test('should set display-name to meta name', t => { +test('should set display-name to meta name', (t) => { t.plan(2) const functionName = 'superDuperSpecialFunction' @@ -189,21 +186,21 @@ test('should set display-name to meta name', t => { name: functionName }) - t.equal(fn[Symbol.for('plugin-meta')].name, functionName) - t.equal(fn[Symbol.for('fastify.display-name')], functionName) + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, functionName) + t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], functionName) }) -test('should preserve fastify version in meta', t => { +test('should preserve fastify version in meta', (t) => { t.plan(1) const opts = { hello: 'world', fastify: '>=0.10.0' } const fn = fp((fastify, opts, next) => next(), opts) - t.equal(fn[Symbol.for('plugin-meta')].fastify, '>=0.10.0') + t.assert.strictEqual(fn[Symbol.for('plugin-meta')].fastify, '>=0.10.0') }) -test('should check fastify dependency graph - plugin', t => { +test('should check fastify dependency graph - plugin', async (t) => { t.plan(1) const fastify = Fastify() @@ -218,12 +215,10 @@ test('should check fastify dependency graph - plugin', t => { dependencies: ['plugin1-name', 'plugin2-name'] })) - fastify.ready(err => { - t.equal(err.message, "The dependency 'plugin2-name' of plugin 'test' is not registered") - }) + await t.assert.rejects(fastify.ready(), { message: "The dependency 'plugin2-name' of plugin 'test' is not registered" }) }) -test('should check fastify dependency graph - decorate', t => { +test('should check fastify dependency graph - decorate', async (t) => { t.plan(1) const fastify = Fastify() @@ -238,12 +233,10 @@ test('should check fastify dependency graph - decorate', t => { decorators: { fastify: ['plugin1', 'plugin2'] } })) - fastify.ready(err => { - t.equal(err.message, "The decorator 'plugin2' required by 'test' is not present in Fastify") - }) + await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Fastify" }) }) -test('should check fastify dependency graph - decorateReply', t => { +test('should check fastify dependency graph - decorateReply', async (t) => { t.plan(1) const fastify = Fastify() @@ -258,13 +251,12 @@ test('should check fastify dependency graph - decorateReply', t => { decorators: { reply: ['plugin1', 'plugin2'] } })) - fastify.ready(err => { - t.equal(err.message, "The decorator 'plugin2' required by 'test' is not present in Reply") - }) + await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Reply" }) }) -test('should accept an option to encapsulate', t => { - t.plan(4) +test('should accept an option to encapsulate', async (t) => { + t.plan(3) + const fastify = Fastify() fastify.register(fp((fastify, opts, next) => { @@ -290,15 +282,14 @@ test('should accept an option to encapsulate', t => { encapsulate: true })) - fastify.ready(err => { - t.error(err) - t.ok(fastify.hasDecorator('accessible')) - t.ok(fastify.hasDecorator('alsoAccessible')) - t.notOk(fastify.hasDecorator('encapsulated')) - }) + await fastify.ready() + + t.assert.ok(fastify.hasDecorator('accessible')) + t.assert.ok(fastify.hasDecorator('alsoAccessible')) + t.assert.ok(!fastify.hasDecorator('encapsulated')) }) -test('should check dependencies when encapsulated', t => { +test('should check dependencies when encapsulated', async (t) => { t.plan(1) const fastify = Fastify() @@ -308,15 +299,13 @@ test('should check dependencies when encapsulated', t => { encapsulate: true })) - fastify.ready(err => { - t.equal(err.message, "The dependency 'missing-dependency-name' of plugin 'test' is not registered") - }) + await t.assert.rejects(fastify.ready(), { message: "The dependency 'missing-dependency-name' of plugin 'test' is not registered" }) }) test( 'should check version when encapsulated', { skip: /\d-.+/.test(pkg.devDependencies.fastify) }, - t => { + async (t) => { t.plan(1) const fastify = Fastify() @@ -326,13 +315,11 @@ test( encapsulate: true })) - fastify.ready(err => { - t.match(err.message, /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d+.\d+' is installed/) - }) + await t.assert.rejects(fastify.ready(), { message: /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d+.\d+' is installed/ }) } ) -test('should check decorators when encapsulated', t => { +test('should check decorators when encapsulated', async (t) => { t.plan(1) const fastify = Fastify() @@ -345,12 +332,11 @@ test('should check decorators when encapsulated', t => { decorators: { fastify: ['plugin1', 'plugin2'] } })) - fastify.ready(err => { - t.equal(err.message, "The decorator 'plugin2' required by 'test' is not present in Fastify") - }) + await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Fastify" }) }) -test('plugin name when encapsulated', async t => { +test('plugin name when encapsulated', async (t) => { + t.plan(6) const fastify = Fastify() fastify.register(function plugin (instance, opts, next) { @@ -371,7 +357,7 @@ test('plugin name when encapsulated', async t => { })) fastify.register(fp(function genericPlugin (fastify, opts, next) { - t.equal(fastify.pluginName, 'deep-deep', 'should be deep-deep') + t.assert.strictEqual(fastify.pluginName, 'deep-deep', 'should be deep-deep') fastify.register(fp(getFn('deep-deep-deep'), { fastify: '5.x', @@ -403,7 +389,7 @@ test('plugin name when encapsulated', async t => { function getFn (expectedName) { return function genericPlugin (fastify, opts, next) { - t.equal(fastify.pluginName, expectedName, `should be ${expectedName}`) + t.assert.strictEqual(fastify.pluginName, expectedName, `should be ${expectedName}`) next() } } diff --git a/test/toCamelCase.test.js b/test/toCamelCase.test.js index 9bb6e25..0cc02db 100644 --- a/test/toCamelCase.test.js +++ b/test/toCamelCase.test.js @@ -1,24 +1,24 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const toCamelCase = require('../lib/toCamelCase') test('from kebab-case to camelCase', (t) => { t.plan(1) - t.equal(toCamelCase('hello-world'), 'helloWorld') + t.assert.strictEqual(toCamelCase('hello-world'), 'helloWorld') }) test('from @-prefixed named imports', (t) => { t.plan(1) - t.equal(toCamelCase('@hello/world'), 'helloWorld') + t.assert.strictEqual(toCamelCase('@hello/world'), 'helloWorld') }) test('from @-prefixed named kebab-case to camelCase', (t) => { t.plan(1) - t.equal(toCamelCase('@hello/my-world'), 'helloMyWorld') + t.assert.strictEqual(toCamelCase('@hello/my-world'), 'helloMyWorld') }) test('from kebab-case to camelCase multiple words', (t) => { t.plan(1) - t.equal(toCamelCase('hello-long-world'), 'helloLongWorld') + t.assert.strictEqual(toCamelCase('hello-long-world'), 'helloLongWorld') })