Skip to content

Commit 9481e92

Browse files
nimesh0505simoneb
andauthored
fix: migrate from tap to node test and c8 (#248)
* 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 <[email protected]>
1 parent 97ec7bc commit 9481e92

10 files changed

+117
-154
lines changed

.taprc

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"scripts": {
99
"lint": "standard",
1010
"test": "npm run test:unit && npm run test:typescript",
11-
"test:unit": "tap",
11+
"test:unit": "c8 --100 node --test",
12+
"test:coverage": "c8 node --test && c8 report --reporter=html",
1213
"test:typescript": "tsd"
1314
},
1415
"repository": {
@@ -30,10 +31,10 @@
3031
"@fastify/pre-commit": "^2.1.0",
3132
"@fastify/type-provider-typebox": "^5.0.0-pre.fv5.1",
3233
"@types/node": "^22.0.0",
34+
"c8": "^10.1.2",
3335
"fastify": "^5.0.0-alpha.2",
3436
"proxyquire": "^2.1.3",
3537
"standard": "^17.1.0",
36-
"tap": "^18.7.0",
3738
"tsd": "^0.31.0"
3839
}
3940
}

test/bundlers.test.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const fp = require('../plugin')
55

6-
test('webpack removes require.main.filename', (t) => {
6+
test('webpack removes require.main.filename', t => {
77
const filename = require.main.filename
88
const info = console.info
9-
t.teardown(() => {
9+
t.after(() => {
1010
require.main.filename = filename
1111
console.info = info
1212
})
1313

1414
require.main.filename = null
1515

1616
console.info = function (msg) {
17-
t.fail('logged: ' + msg)
17+
t.assert.fail('logged: ' + msg)
1818
}
1919

2020
fp((fastify, opts, next) => {
2121
next()
2222
}, {
2323
fastify: '^5.0.0'
2424
})
25-
26-
t.end()
2725
})
2826

2927
test('support faux modules', (t) => {
3028
const plugin = fp((fastify, opts, next) => {
3129
next()
3230
})
3331

34-
t.equal(plugin.default, plugin)
35-
t.end()
32+
t.assert.strictEqual(plugin.default, plugin)
3633
})
3734

3835
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
4441

4542
const plugin = fp(module)
4643

47-
t.equal(plugin.default, 'Existing default field')
48-
t.end()
44+
t.assert.strictEqual(plugin.default, 'Existing default field')
4945
})
5046

5147
test('support ts named imports', (t) => {
@@ -55,8 +51,7 @@ test('support ts named imports', (t) => {
5551
name: 'hello'
5652
})
5753

58-
t.equal(plugin.hello, plugin)
59-
t.end()
54+
t.assert.strictEqual(plugin.hello, plugin)
6055
})
6156

6257
test('from kebab-case to camelCase', (t) => {
@@ -66,8 +61,7 @@ test('from kebab-case to camelCase', (t) => {
6661
name: 'hello-world'
6762
})
6863

69-
t.equal(plugin.helloWorld, plugin)
70-
t.end()
64+
t.assert.strictEqual(plugin.helloWorld, plugin)
7165
})
7266

7367
test('from @-prefixed named imports', (t) => {
@@ -77,8 +71,7 @@ test('from @-prefixed named imports', (t) => {
7771
name: '@hello/world'
7872
})
7973

80-
t.equal(plugin.helloWorld, plugin)
81-
t.end()
74+
t.assert.strictEqual(plugin.helloWorld, plugin)
8275
})
8376

8477
test('from @-prefixed named kebab-case to camelCase', (t) => {
@@ -88,8 +81,7 @@ test('from @-prefixed named kebab-case to camelCase', (t) => {
8881
name: '@hello/my-world'
8982
})
9083

91-
t.equal(plugin.helloMyWorld, plugin)
92-
t.end()
84+
t.assert.strictEqual(plugin.helloMyWorld, plugin)
9385
})
9486

9587
test('from kebab-case to camelCase multiple words', (t) => {
@@ -99,8 +91,7 @@ test('from kebab-case to camelCase multiple words', (t) => {
9991
name: 'hello-long-world'
10092
})
10193

102-
t.equal(plugin.helloLongWorld, plugin)
103-
t.end()
94+
t.assert.strictEqual(plugin.helloLongWorld, plugin)
10495
})
10596

10697
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) => {
115106
name: 'hello-long-world'
116107
})
117108

118-
t.equal(plugin.helloLongWorld, foobar)
119-
t.end()
109+
t.assert.strictEqual(plugin.helloLongWorld, foobar)
120110
})

test/checkVersion.test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const fp = require('../plugin')
55

66
test('checkVersion having require.main.filename', (t) => {
77
const info = console.info
8-
t.ok(require.main.filename)
9-
t.teardown(() => {
8+
t.assert.ok(require.main.filename)
9+
t.after(() => {
1010
console.info = info
1111
})
1212

1313
console.info = function (msg) {
14-
t.fail('logged: ' + msg)
14+
t.assert.fail('logged: ' + msg)
1515
}
1616

1717
fp((fastify, opts, next) => {
1818
next()
1919
}, {
2020
fastify: '^5.0.0'
2121
})
22-
23-
t.end()
2422
})
2523

2624
test('checkVersion having no require.main.filename but process.argv[1]', (t) => {
2725
const filename = require.main.filename
2826
const info = console.info
29-
t.teardown(() => {
27+
t.after(() => {
3028
require.main.filename = filename
3129
console.info = info
3230
})
3331

3432
require.main.filename = null
3533

3634
console.info = function (msg) {
37-
t.fail('logged: ' + msg)
35+
t.assert.fail('logged: ' + msg)
3836
}
3937

4038
fp((fastify, opts, next) => {
4139
next()
4240
}, {
4341
fastify: '^5.0.0'
4442
})
45-
46-
t.end()
4743
})
4844

4945
test('checkVersion having no require.main.filename and no process.argv[1]', (t) => {
5046
const filename = require.main.filename
5147
const argv = process.argv
5248
const info = console.info
53-
t.teardown(() => {
49+
t.after(() => {
5450
require.main.filename = filename
5551
process.argv = argv
5652
console.info = info
@@ -60,14 +56,12 @@ test('checkVersion having no require.main.filename and no process.argv[1]', (t)
6056
process.argv[1] = null
6157

6258
console.info = function (msg) {
63-
t.fail('logged: ' + msg)
59+
t.assert.fail('logged: ' + msg)
6460
}
6561

6662
fp((fastify, opts, next) => {
6763
next()
6864
}, {
6965
fastify: '^5.0.0'
7066
})
71-
72-
t.end()
7367
})

test/composite.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict'
22

3-
const t = require('tap')
4-
const test = t.test
3+
const { test } = require('node:test')
54
const fp = require('../plugin')
65

7-
test('anonymous function should be named composite.test0', t => {
6+
test('anonymous function should be named composite.test0', (t) => {
87
t.plan(2)
9-
108
const fn = fp((fastify, opts, next) => {
119
next()
1210
})
1311

14-
t.equal(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0')
15-
t.equal(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0')
12+
t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'composite.test-auto-0')
13+
t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'composite.test-auto-0')
1614
})

test/esm/esm.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import t from 'tap'
2-
1+
import { test } from 'node:test'
32
import fp from '../../plugin.js'
43

5-
t.test('esm base support', async t => {
4+
test('esm base support', (t) => {
65
fp((fastify, opts, next) => {
76
next()
87
}, {
98
fastify: '^5.0.0'
109
})
11-
12-
t.end()
10+
t.assert.ok(true, 'fp function called without throwing an error')
1311
})

test/extractPluginName.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const t = require('tap')
3+
const { test } = require('node:test')
44
const extractPluginName = require('../lib/getPluginName').extractPluginName
55

66
const winStack = `Error: anonymous function
@@ -41,8 +41,9 @@ at TAP.test (/home/leonardo/desktop/fastify-plugin/node_modules/tap/lib/test.js:
4141

4242
const anonymousStack = 'Unable to parse this'
4343

44-
t.plan(3)
45-
46-
t.equal(extractPluginName(winStack), 'hello.test')
47-
t.equal(extractPluginName(nixStack), 'this.is.a.test')
48-
t.equal(extractPluginName(anonymousStack), 'anonymous')
44+
test('extractPluginName tests', (t) => {
45+
t.plan(3)
46+
t.assert.strictEqual(extractPluginName(winStack), 'hello.test')
47+
t.assert.strictEqual(extractPluginName(nixStack), 'this.is.a.test')
48+
t.assert.strictEqual(extractPluginName(anonymousStack), 'anonymous')
49+
})

test/mu1tip1e.composite.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
'use strict'
22

3-
const t = require('tap')
4-
const test = t.test
3+
const { test } = require('node:test')
54
const fp = require('../plugin')
65

7-
test('anonymous function should be named mu1tip1e.composite.test', t => {
6+
test('anonymous function should be named mu1tip1e.composite.test', (t) => {
87
t.plan(2)
98

109
const fn = fp((fastify, opts, next) => {
1110
next()
1211
})
1312

14-
t.equal(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0')
15-
t.equal(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0')
13+
t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test-auto-0')
14+
t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test-auto-0')
1615
})

0 commit comments

Comments
 (0)