Skip to content

Commit 36bd0e9

Browse files
authored
chore: migrate from tap to node:test and c8 (#150)
* fix: migrate from tap to node:test and c8 * fix: c8 to dev dependencies
1 parent 79372bf commit 36bd0e9

File tree

4 files changed

+95
-94
lines changed

4 files changed

+95
-94
lines changed

.taprc

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint:fix": "standard --verbose --fix | snazzy",
1111
"test": "npm run test:unit && npm run test:typescript",
1212
"test:typescript": "tsd",
13-
"test:unit": "tap",
13+
"test:unit": "c8 --100 node --test",
1414
"test:unit:report": "npm run test:unit -- --coverage-report=html",
1515
"test:unit:verbose": "npm run test:unit -- -Rspec"
1616
},
@@ -36,10 +36,10 @@
3636
"devDependencies": {
3737
"@fastify/pre-commit": "^2.1.0",
3838
"@types/node": "^22.0.0",
39+
"c8": "^10.1.2",
3940
"fastify": "^5.0.0-alpha.3",
4041
"snazzy": "^9.0.0",
4142
"standard": "^17.1.0",
42-
"tap": "^18.7.0",
4343
"tsd": "^0.31.0"
4444
},
4545
"dependencies": {

test/cache.test.js

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

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

66
const Fastify = require('fastify')
7+
const { setTimeout: sleep } = require('node:timers/promises')
78

89
test('cache property gets added to instance', async (t) => {
910
t.plan(2)
@@ -12,8 +13,8 @@ test('cache property gets added to instance', async (t) => {
1213
await fastify.register(plugin)
1314
await fastify.ready()
1415

15-
t.ok(fastify.cache)
16-
t.ok(fastify.cache.set)
16+
t.assert.ok(fastify.cache)
17+
t.assert.ok(fastify.cache.set)
1718
})
1819

1920
test('cache is usable', async (t) => {
@@ -22,13 +23,13 @@ test('cache is usable', async (t) => {
2223
const fastify = Fastify()
2324
await fastify.register(async (instance, options) => {
2425
instance.addHook('onRequest', async function (req, reply) {
25-
t.notOk(instance[Symbol.for('fastify-caching.registered')])
26+
t.assert.ifError(instance[Symbol.for('fastify-caching.registered')])
2627
})
2728
})
2829
await fastify.register(plugin)
2930

3031
fastify.addHook('onRequest', async function (req, reply) {
31-
t.equal(this[Symbol.for('fastify-caching.registered')], true)
32+
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
3233
})
3334

3435
fastify.get('/one', (req, reply) => {
@@ -40,9 +41,8 @@ test('cache is usable', async (t) => {
4041

4142
fastify.get('/two', (req, reply) => {
4243
fastify.cache.get('one', (err, obj) => {
43-
t.error(err)
44-
t.same(obj.item, { one: true })
45-
44+
t.assert.ifError(err)
45+
t.assert.strictEqual(obj.item, { one: true })
4646
reply.send()
4747
})
4848
})
@@ -72,13 +72,13 @@ test('cache is usable with function as plugin default options input', async (t)
7272
const fastify = Fastify()
7373
await fastify.register(async (instance, options) => {
7474
instance.addHook('onRequest', async function (req, reply) {
75-
t.notOk(instance[Symbol.for('fastify-caching.registered')])
75+
t.assert.failure(instance[Symbol.for('fastify-caching.registered')])
7676
})
7777
})
78-
await fastify.register(plugin, () => () => { })
78+
await fastify.register(plugin, () => () => {})
7979

8080
fastify.addHook('onRequest', async function (req, reply) {
81-
t.equal(this[Symbol.for('fastify-caching.registered')], true)
81+
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
8282
})
8383

8484
fastify.get('/one', (req, reply) => {
@@ -90,8 +90,8 @@ test('cache is usable with function as plugin default options input', async (t)
9090

9191
fastify.get('/two', (req, reply) => {
9292
fastify.cache.get('one', (err, obj) => {
93-
t.error(err)
94-
t.same(obj.item, { one: true })
93+
t.assert.ifError(err)
94+
t.assert.strictEqual(obj.item, { one: true })
9595

9696
reply.send()
9797
})
@@ -130,16 +130,14 @@ test('getting cache item with error returns error', async (t) => {
130130
fastify.get('/one', (req, reply) => {
131131
fastify.cache.set('one', { one: true }, 1000, (err) => {
132132
if (err) return reply.send(err)
133-
return reply
134-
.etag('123456')
135-
.send({ hello: 'world' })
133+
return reply.etag('123456').send({ hello: 'world' })
136134
})
137135
})
138136

139137
fastify.get('/two', (req, reply) => {
140138
fastify.cache.get('one', (err, obj) => {
141-
t.notOk(err)
142-
t.notOk(obj)
139+
t.assert.failure(err)
140+
t.assert.failure(obj)
143141
})
144142
})
145143

@@ -157,7 +155,7 @@ test('getting cache item with error returns error', async (t) => {
157155
'if-none-match': '123456'
158156
}
159157
})
160-
t.equal(response.statusCode, 500)
158+
t.assert.strictEqual(response.statusCode, 500)
161159
})
162160

163161
test('etags get stored in cache', async (t) => {
@@ -167,9 +165,7 @@ test('etags get stored in cache', async (t) => {
167165
await fastify.register(plugin)
168166

169167
fastify.get('/one', (req, reply) => {
170-
reply
171-
.etag('123456')
172-
.send({ hello: 'world' })
168+
reply.etag('123456').send({ hello: 'world' })
173169
})
174170

175171
await fastify.ready()
@@ -186,14 +182,14 @@ test('etags get stored in cache', async (t) => {
186182
'if-none-match': '123456'
187183
}
188184
})
189-
t.equal(response.statusCode, 304)
185+
t.assert.strictEqual(response.statusCode, 304)
190186
})
191187

192-
test('etag cache life is customizable', (t) => {
188+
test('etag cache life is customizable', async (t) => {
193189
t.plan(4)
194190

195191
const fastify = Fastify()
196-
fastify.register(plugin)
192+
await fastify.register(plugin)
197193

198194
fastify.get('/one', function (req, reply) {
199195
reply
@@ -203,29 +199,36 @@ test('etag cache life is customizable', (t) => {
203199
})
204200

205201
fastify.ready((err) => {
206-
t.error(err)
207-
208-
fastify.inject({
209-
method: 'GET',
210-
path: '/one'
211-
}, (err, _response) => {
212-
t.error(err)
213-
214-
// We wait 70 milliseconds that the cache expires
215-
setTimeout(() => {
216-
fastify.inject({
217-
method: 'GET',
218-
path: '/one',
219-
headers: {
220-
'if-none-match': '123456'
221-
}
222-
}, (err, response) => {
223-
t.error(err)
224-
t.equal(response.statusCode, 200)
225-
})
226-
}, 70)
227-
})
202+
t.assert.ifError(err)
203+
204+
fastify.inject(
205+
{
206+
method: 'GET',
207+
path: '/one'
208+
},
209+
(err, _response) => {
210+
t.assert.ifError(err)
211+
212+
// We wait 70 milliseconds that the cache expires
213+
setTimeout(() => {
214+
fastify.inject(
215+
{
216+
method: 'GET',
217+
path: '/one',
218+
headers: {
219+
'if-none-match': '123456'
220+
}
221+
},
222+
(err, response) => {
223+
t.assert.ifError(err)
224+
t.assert.strictEqual(response.statusCode, 200)
225+
}
226+
)
227+
}, 70)
228+
}
229+
)
228230
})
231+
await sleep(100)
229232
})
230233

231234
test('returns response payload', async (t) => {
@@ -235,9 +238,7 @@ test('returns response payload', async (t) => {
235238
await fastify.register(plugin)
236239

237240
fastify.get('/one', (req, reply) => {
238-
reply
239-
.etag('123456', 300)
240-
.send({ hello: 'world' })
241+
reply.etag('123456', 300).send({ hello: 'world' })
241242
})
242243

243244
await fastify.ready()
@@ -252,5 +253,5 @@ test('returns response payload', async (t) => {
252253
path: '/one'
253254
})
254255

255-
t.same(JSON.parse(response.payload), { hello: 'world' })
256+
t.assert.deepStrictEqual(JSON.parse(response.payload), { hello: 'world' })
256257
})

0 commit comments

Comments
 (0)