Skip to content

Commit

Permalink
fix: migrate from tap to node:test and c8
Browse files Browse the repository at this point in the history
  • Loading branch information
dancastillo committed Aug 18, 2024
1 parent 79372bf commit 59cd364
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 94 deletions.
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "standard --verbose --fix | snazzy",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap",
"test:unit": "c8 --100 node --test",
"test:unit:report": "npm run test:unit -- --coverage-report=html",
"test:unit:verbose": "npm run test:unit -- -Rspec"
},
Expand Down Expand Up @@ -39,11 +39,11 @@
"fastify": "^5.0.0-alpha.3",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^18.7.0",
"tsd": "^0.31.0"
},
"dependencies": {
"abstract-cache": "^1.0.1",
"c8": "^10.1.2",
"fastify-plugin": "^5.0.0-pre.fv5.1",
"uid-safe": "^2.1.5"
},
Expand Down
103 changes: 52 additions & 51 deletions test/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const plugin = require('..')

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

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

t.ok(fastify.cache)
t.ok(fastify.cache.set)
t.assert.ok(fastify.cache)
t.assert.ok(fastify.cache.set)
})

test('cache is usable', async (t) => {
Expand All @@ -22,13 +23,13 @@ test('cache is usable', async (t) => {
const fastify = Fastify()
await fastify.register(async (instance, options) => {
instance.addHook('onRequest', async function (req, reply) {
t.notOk(instance[Symbol.for('fastify-caching.registered')])
t.assert.ifError(instance[Symbol.for('fastify-caching.registered')])
})
})
await fastify.register(plugin)

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

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

fastify.get('/two', (req, reply) => {
fastify.cache.get('one', (err, obj) => {
t.error(err)
t.same(obj.item, { one: true })

t.assert.ifError(err)
t.assert.strictEqual(obj.item, { one: true })
reply.send()
})
})
Expand Down Expand Up @@ -72,13 +72,13 @@ test('cache is usable with function as plugin default options input', async (t)
const fastify = Fastify()
await fastify.register(async (instance, options) => {
instance.addHook('onRequest', async function (req, reply) {
t.notOk(instance[Symbol.for('fastify-caching.registered')])
t.assert.failure(instance[Symbol.for('fastify-caching.registered')])
})
})
await fastify.register(plugin, () => () => { })
await fastify.register(plugin, () => () => {})

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

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

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

reply.send()
})
Expand Down Expand Up @@ -130,16 +130,14 @@ test('getting cache item with error returns error', async (t) => {
fastify.get('/one', (req, reply) => {
fastify.cache.set('one', { one: true }, 1000, (err) => {
if (err) return reply.send(err)
return reply
.etag('123456')
.send({ hello: 'world' })
return reply.etag('123456').send({ hello: 'world' })
})
})

fastify.get('/two', (req, reply) => {
fastify.cache.get('one', (err, obj) => {
t.notOk(err)
t.notOk(obj)
t.assert.failure(err)
t.assert.failure(obj)
})
})

Expand All @@ -157,7 +155,7 @@ test('getting cache item with error returns error', async (t) => {
'if-none-match': '123456'
}
})
t.equal(response.statusCode, 500)
t.assert.strictEqual(response.statusCode, 500)
})

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

fastify.get('/one', (req, reply) => {
reply
.etag('123456')
.send({ hello: 'world' })
reply.etag('123456').send({ hello: 'world' })
})

await fastify.ready()
Expand All @@ -186,14 +182,14 @@ test('etags get stored in cache', async (t) => {
'if-none-match': '123456'
}
})
t.equal(response.statusCode, 304)
t.assert.strictEqual(response.statusCode, 304)
})

test('etag cache life is customizable', (t) => {
test('etag cache life is customizable', async (t) => {
t.plan(4)

const fastify = Fastify()
fastify.register(plugin)
await fastify.register(plugin)

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

fastify.ready((err) => {
t.error(err)

fastify.inject({
method: 'GET',
path: '/one'
}, (err, _response) => {
t.error(err)

// We wait 70 milliseconds that the cache expires
setTimeout(() => {
fastify.inject({
method: 'GET',
path: '/one',
headers: {
'if-none-match': '123456'
}
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 200)
})
}, 70)
})
t.assert.ifError(err)

fastify.inject(
{
method: 'GET',
path: '/one'
},
(err, _response) => {
t.assert.ifError(err)

// We wait 70 milliseconds that the cache expires
setTimeout(() => {
fastify.inject(
{
method: 'GET',
path: '/one',
headers: {
'if-none-match': '123456'
}
},
(err, response) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
}
)
}, 70)
}
)
})
await sleep(100)
})

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

fastify.get('/one', (req, reply) => {
reply
.etag('123456', 300)
.send({ hello: 'world' })
reply.etag('123456', 300).send({ hello: 'world' })
})

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

t.same(JSON.parse(response.payload), { hello: 'world' })
t.assert.deepStrictEqual(JSON.parse(response.payload), { hello: 'world' })
})
Loading

0 comments on commit 59cd364

Please sign in to comment.