Skip to content

Commit

Permalink
fix: migrate from tap to node test and c8 (#248)
Browse files Browse the repository at this point in the history
* migrate test suite from tap to node:test

* update c8 coverage to 100%

* migrate ESM test from tap to node:test

* test: remove unnecessary async from test functions

---------

Co-authored-by: Simone Busoli <simone.busoli@gmail.com>
  • Loading branch information
nimesh0505 and simoneb authored Sep 15, 2024
1 parent 97ec7bc commit 9481e92
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 154 deletions.
4 changes: 0 additions & 4 deletions .taprc

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
34 changes: 12 additions & 22 deletions test/bundlers.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
'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
})

require.main.filename = null

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})

test('support faux modules', (t) => {
const plugin = fp((fastify, opts, next) => {
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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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)
})
22 changes: 8 additions & 14 deletions test/checkVersion.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
'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) => {
next()
}, {
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
})

require.main.filename = null

console.info = function (msg) {
t.fail('logged: ' + msg)
t.assert.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
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
Expand All @@ -60,14 +56,12 @@ 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) => {
next()
}, {
fastify: '^5.0.0'
})

t.end()
})
10 changes: 4 additions & 6 deletions test/composite.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
8 changes: 3 additions & 5 deletions test/esm/esm.mjs
Original file line number Diff line number Diff line change
@@ -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')
})
13 changes: 7 additions & 6 deletions test/extractPluginName.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
})
9 changes: 4 additions & 5 deletions test/mu1tip1e.composite.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
Loading

0 comments on commit 9481e92

Please sign in to comment.