From 59cd36450b48be892f0252e19928da7ddaa0a6dc Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Sun, 18 Aug 2024 14:54:40 -0400 Subject: [PATCH 1/2] fix: migrate from tap to node:test and c8 --- .taprc | 2 - package.json | 4 +- test/cache.test.js | 103 ++++++++++++++++++++++--------------------- test/headers.test.js | 80 +++++++++++++++++---------------- 4 files changed, 95 insertions(+), 94 deletions(-) delete mode 100644 .taprc diff --git a/.taprc b/.taprc deleted file mode 100644 index eb6eb3e..0000000 --- a/.taprc +++ /dev/null @@ -1,2 +0,0 @@ -files: - - test/**/*.test.js diff --git a/package.json b/package.json index 2fbca63..6c470f2 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" }, diff --git a/test/cache.test.js b/test/cache.test.js index 0770b80..97c3a5a 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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) @@ -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) => { @@ -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) => { @@ -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() }) }) @@ -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) => { @@ -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() }) @@ -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) }) }) @@ -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) => { @@ -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() @@ -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 @@ -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) => { @@ -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() @@ -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' }) }) diff --git a/test/headers.test.js b/test/headers.test.js index 9afd1ea..6c1e3b8 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const Fastify = require('fastify') const plugin = require('..') @@ -11,7 +11,7 @@ test('decorators get added', async (t) => { await fastify.register(plugin) fastify.get('/', (req, reply) => { - t.ok(reply.etag) + t.assert.ok(reply.etag) reply.send() }) @@ -32,9 +32,7 @@ test('decorators add headers', async (t) => { await fastify.register(plugin) fastify.get('/', (req, reply) => { - reply - .etag(tag) - .send() + reply.etag(tag).send() }) await fastify.ready() @@ -43,8 +41,8 @@ test('decorators add headers', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers.etag) - t.equal(response.headers.etag, tag) + t.assert.ok(response.headers.etag) + t.assert.strictEqual(response.headers.etag, tag) }) test('sets etag header for falsy argument', async (t) => { @@ -54,9 +52,7 @@ test('sets etag header for falsy argument', async (t) => { await fastify.register(plugin) fastify.get('/', (req, reply) => { - reply - .etag() - .send() + reply.etag().send() }) await fastify.ready() @@ -65,7 +61,7 @@ test('sets etag header for falsy argument', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers.etag) + t.assert.ok(response.headers.etag) }) test('sets no-cache header', async (t) => { @@ -84,8 +80,8 @@ test('sets no-cache header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'no-cache') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual(response.headers['cache-control'], 'no-cache') }) test('sets private with max-age header', async (t) => { @@ -109,8 +105,11 @@ test('sets private with max-age header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'private, max-age=300') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual( + response.headers['cache-control'], + 'private, max-age=300' + ) }) test('sets public with max-age and s-maxage header', async (t) => { @@ -135,8 +134,11 @@ test('sets public with max-age and s-maxage header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'public, max-age=300, s-maxage=12345') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual( + response.headers['cache-control'], + 'public, max-age=300, s-maxage=12345' + ) }) test('do not set headers if another upstream plugin already sets it', async (t) => { @@ -164,8 +166,8 @@ test('do not set headers if another upstream plugin already sets it', async (t) method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'do not override') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual(response.headers['cache-control'], 'do not override') }) test('only sets max-age and ignores s-maxage with private header', async (t) => { @@ -190,8 +192,11 @@ test('only sets max-age and ignores s-maxage with private header', async (t) => method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'private, max-age=300') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual( + response.headers['cache-control'], + 'private, max-age=300' + ) }) test('s-maxage is optional with public header', async (t) => { @@ -215,8 +220,8 @@ test('s-maxage is optional with public header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'public, max-age=300') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual(response.headers['cache-control'], 'public, max-age=300') }) test('sets no-store with max-age header', async (t) => { @@ -235,8 +240,11 @@ test('sets no-store with max-age header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers['cache-control']) - t.equal(response.headers['cache-control'], 'no-store, max-age=300') + t.assert.ok(response.headers['cache-control']) + t.assert.strictEqual( + response.headers['cache-control'], + 'no-store, max-age=300' + ) }) test('sets the expires header', async (t) => { @@ -248,9 +256,7 @@ test('sets the expires header', async (t) => { await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) fastify.get('/', (req, reply) => { - reply - .expires(now) - .send({ hello: 'world' }) + reply.expires(now).send({ hello: 'world' }) }) await fastify.ready() @@ -259,8 +265,8 @@ test('sets the expires header', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers.expires) - t.equal(response.headers.expires, now.toUTCString()) + t.assert.ok(response.headers.expires) + t.assert.strictEqual(response.headers.expires, now.toUTCString()) }) test('sets the expires header to a falsy value', async (t) => { @@ -270,9 +276,7 @@ test('sets the expires header to a falsy value', async (t) => { await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) fastify.get('/', (req, reply) => { - reply - .expires() - .send({ hello: 'world' }) + reply.expires().send({ hello: 'world' }) }) await fastify.ready() @@ -281,7 +285,7 @@ test('sets the expires header to a falsy value', async (t) => { method: 'GET', path: '/' }) - t.notOk(response.headers.expires) + t.assert.ifError(response.headers.expires) }) test('sets the expires header to a custom value', async (t) => { @@ -291,9 +295,7 @@ test('sets the expires header to a custom value', async (t) => { await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE }) fastify.get('/', (req, reply) => { - reply - .expires('foobar') - .send({ hello: 'world' }) + reply.expires('foobar').send({ hello: 'world' }) }) await fastify.ready() @@ -302,6 +304,6 @@ test('sets the expires header to a custom value', async (t) => { method: 'GET', path: '/' }) - t.ok(response.headers.expires) - t.equal(response.headers.expires, 'foobar') + t.assert.ok(response.headers.expires) + t.assert.strictEqual(response.headers.expires, 'foobar') }) From 86573fe51c70f003c396c6d9b0689b11f7f50b06 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Sun, 18 Aug 2024 15:41:39 -0400 Subject: [PATCH 2/2] fix: c8 to dev dependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6c470f2..a049c8a 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "devDependencies": { "@fastify/pre-commit": "^2.1.0", "@types/node": "^22.0.0", + "c8": "^10.1.2", "fastify": "^5.0.0-alpha.3", "snazzy": "^9.0.0", "standard": "^17.1.0", @@ -43,7 +44,6 @@ }, "dependencies": { "abstract-cache": "^1.0.1", - "c8": "^10.1.2", "fastify-plugin": "^5.0.0-pre.fv5.1", "uid-safe": "^2.1.5" },