From e4ae352c432437d94c1bf34b96991a540a247dc2 Mon Sep 17 00:00:00 2001 From: SamSalvatico <40636569+SamSalvatico@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:19:02 +0100 Subject: [PATCH] test: #5555 - move to node test runner (#440) * test: forceEsm from tap to node test runner * test: updated test/typescript/basic.ts * test: updated test/commonjs/autohooks-basic.js * test: updated test/commonjs/autohooks-*.js * test: updated test/commonjs/babel-node.js and basic.js * test: updated cyclic * test: updated deep * test: updated dependency * test: updated commonjs/error * test: updated graph-dependency * test: updated commonjs/index-package.js * test: updated commonjs/non-plugin.js * test: updated commonjs/options.js * test: updated commonjs/route-parametrs.js * test: use promises in forceEsm * test: removed assert if error after promises * test: removed nested suites * test: updated issue 369 * test: updated issue 374 * test: updated issue 376 * Revert "test: updated issue 376" This reverts commit 71a6660fbf94f548a371afebdb8cf96fd87ad64a. * test: updated issue 376 * test: updated issue 388 * test: updated module/autohooks * test: updated module/basic * test: updated module/dependency * test: updated module/route-parameters * test: updated module/index-package * test: updated module/options * test: fixed /route-parameters * test: updated module/esm-import * chore: removed tap from package.json * test: added glob to make windows able to expand patterns * test: try using fast-glob instead of glob * test: use borp instead of fast-glob script * test: readded coverage * test: added check on code coverage * chore: newline --- .borp.yaml | 4 + .taprc | 4 - package.json | 7 +- scripts/unit-typescript-esm.js | 9 +- scripts/unit-typescript-tsm.js | 1 + scripts/unit.js | 9 - test/commonjs/autohooks-basic.js | 61 ++-- test/commonjs/autohooks-cascade.js | 61 ++-- test/commonjs/autohooks-disabled.js | 61 ++-- test/commonjs/autohooks-overwrite.js | 61 ++-- test/commonjs/babel-node.js | 46 +-- test/commonjs/basic.js | 358 +++++++++------------ test/commonjs/cyclic.js | 19 +- test/commonjs/deep.js | 42 +-- test/commonjs/dependency.js | 93 +++--- test/commonjs/error.js | 77 +++-- test/commonjs/graph-dependency.js | 52 +-- test/commonjs/index-package.js | 29 +- test/commonjs/non-plugin.js | 30 +- test/commonjs/options.js | 97 +++--- test/commonjs/route-parameters-basic.js | 67 ++-- test/commonjs/route-parameters-disabled.js | 46 ++- test/issues/369/test.js | 331 +++++++++---------- test/issues/374/test.js | 65 ++-- test/issues/376/test.js | 84 ++--- test/issues/388/test.js | 57 ++-- test/module/autohooks.js | 231 +++++++------ test/module/basic.js | 314 ++++++++---------- test/module/dependency.js | 88 +++-- test/module/esm-import.js | 102 +++--- test/module/index-package.js | 22 +- test/module/options.js | 92 +++--- test/module/route-parameters.js | 183 +++++++---- test/typescript-esm/forceESM.ts | 34 +- test/typescript/basic.ts | 41 ++- 35 files changed, 1430 insertions(+), 1448 deletions(-) create mode 100644 .borp.yaml delete mode 100644 .taprc delete mode 100644 scripts/unit.js diff --git a/.borp.yaml b/.borp.yaml new file mode 100644 index 00000000..4b7968ef --- /dev/null +++ b/.borp.yaml @@ -0,0 +1,4 @@ +files: + - 'test/issues/*/test.js' + - 'test/commonjs/*.js' + - 'test/module/*.js' diff --git a/.taprc b/.taprc deleted file mode 100644 index e2dd1e4d..00000000 --- a/.taprc +++ /dev/null @@ -1,4 +0,0 @@ -# vim: set filetype=yaml : -timeout: 60 -plugin: - - "!@tapjs/typescript" diff --git a/package.json b/package.json index 831731ac..ce5c767d 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,7 @@ "typescript:native": "node scripts/unit-typescript-native-type-stripping.js", "typescript:vitest": "vitest run", "typescript:vitest:dev": "vitest", - "unit": "node scripts/unit.js", - "unit:with-modules": "tap plugin rm @tapjs/typescript && tap plugin list && tap build && tap test/issues/*/test.js test/commonjs/*.js test/module/*.js" + "unit": "borp -C --check-coverage --lines 100 --reporter=@jsumners/line-reporter" }, "repository": { "type": "git", @@ -76,11 +75,12 @@ "devDependencies": { "@fastify/pre-commit": "^2.1.0", "@fastify/url-data": "^6.0.0", + "@jsumners/line-reporter": "^1.0.1", "@swc-node/register": "^1.9.1", "@swc/core": "^1.5.25", "@types/jest": "^29.5.12", "@types/node": "^22.0.0", - "@types/tap": "^18.0.0", + "borp": "^0.19.0", "esbuild": "^0.24.0", "esbuild-register": "^3.5.0", "eslint": "^9.17.0", @@ -88,7 +88,6 @@ "fastify-plugin": "^5.0.0", "jest": "^29.7.0", "neostandard": "^0.12.0", - "tap": "^19.0.2", "ts-jest": "^29.1.4", "ts-node": "^10.9.2", "ts-node-dev": "^2.0.0", diff --git a/scripts/unit-typescript-esm.js b/scripts/unit-typescript-esm.js index 0dd3ea43..f1e8aff8 100644 --- a/scripts/unit-typescript-esm.js +++ b/scripts/unit-typescript-esm.js @@ -3,11 +3,10 @@ const { exec } = require('node:child_process') const args = [ - 'tap', - '--node-arg=--loader=ts-node/esm', - '--node-arg=--experimental-specifier-resolution=node', - '--disable-coverage', - '--allow-empty-coverage', + 'node', + '--loader=ts-node/esm', + '--experimental-specifier-resolution=node', + '--test', 'test/typescript-esm/*.ts' ] diff --git a/scripts/unit-typescript-tsm.js b/scripts/unit-typescript-tsm.js index 0d1a50c3..419c1e17 100644 --- a/scripts/unit-typescript-tsm.js +++ b/scripts/unit-typescript-tsm.js @@ -5,6 +5,7 @@ const { exec } = require('node:child_process') const args = [ 'node', '--require=tsm', + '--test', 'test/typescript/basic.ts' ] diff --git a/scripts/unit.js b/scripts/unit.js deleted file mode 100644 index dd9f74c5..00000000 --- a/scripts/unit.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const { exec } = require('node:child_process') - -const child = exec('npm run unit:with-modules') - -child.stdout.pipe(process.stdout) -child.stderr.pipe(process.stderr) -child.once('close', process.exit) diff --git a/test/commonjs/autohooks-basic.js b/test/commonjs/autohooks-basic.js index 85338cb4..5ece6053 100644 --- a/test/commonjs/autohooks-basic.js +++ b/test/commonjs/autohooks-basic.js @@ -1,51 +1,46 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(13) - -const app = Fastify() - -app.register(require('./autohooks/basic')) -app.decorateRequest('hooked', '') +describe('Node test suite for autohooks-basic', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/basic')) + app.decorateRequest('hooked', '') + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) }) - app.inject({ - url: '/child' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['child'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['child'] }) }) - app.inject({ - url: '/child/grandchild' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject({ url: '/child/grandchild' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['grandchild'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['grandchild'] }) }) - app.inject({ - url: '/sibling' - }, function (err, res) { - t.error(err) + it('should respond correctly to /sibling', async function () { + const res = await app.inject({ url: '/sibling' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: '' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: '' }) }) }) diff --git a/test/commonjs/autohooks-cascade.js b/test/commonjs/autohooks-cascade.js index 82d541a4..356a5040 100644 --- a/test/commonjs/autohooks-cascade.js +++ b/test/commonjs/autohooks-cascade.js @@ -1,51 +1,46 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(13) - -const app = Fastify() - -app.register(require('./autohooks/cascade')) -app.decorateRequest('hooked', '') +describe('Node test suite for autohooks-cascade', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/cascade')) + app.decorateRequest('hooked', '') + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) }) - app.inject({ - url: '/child' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root', 'child'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root', 'child'] }) }) - app.inject({ - url: '/child/grandchild' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject({ url: '/child/grandchild' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root', 'child', 'grandchild'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root', 'child', 'grandchild'] }) }) - app.inject({ - url: '/sibling' - }, function (err, res) { - t.error(err) + it('should respond correctly to /sibling', async function () { + const res = await app.inject({ url: '/sibling' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) }) }) diff --git a/test/commonjs/autohooks-disabled.js b/test/commonjs/autohooks-disabled.js index ad3c9afe..0d87798b 100644 --- a/test/commonjs/autohooks-disabled.js +++ b/test/commonjs/autohooks-disabled.js @@ -1,51 +1,46 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(13) - -const app = Fastify() - -app.register(require('./autohooks/disabled')) -app.decorateRequest('hooked', 'disabled') +describe('Node test suite for autohooks-disabled', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/disabled')) + app.decorateRequest('hooked', 'disabled') + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: 'disabled' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: 'disabled' }) }) - app.inject({ - url: '/child' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: 'disabled' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: 'disabled' }) }) - app.inject({ - url: '/child/grandchild' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject({ url: '/child/grandchild' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: 'disabled' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: 'disabled' }) }) - app.inject({ - url: '/sibling' - }, function (err, res) { - t.error(err) + it('should respond correctly to /sibling', async function () { + const res = await app.inject({ url: '/sibling' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: 'disabled' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: 'disabled' }) }) }) diff --git a/test/commonjs/autohooks-overwrite.js b/test/commonjs/autohooks-overwrite.js index 4c43ffd0..1e662861 100644 --- a/test/commonjs/autohooks-overwrite.js +++ b/test/commonjs/autohooks-overwrite.js @@ -1,51 +1,46 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(13) - -const app = Fastify() - -app.register(require('./autohooks/overwrite')) -app.decorateRequest('hooked', '') +describe('Node test suite for autohooks-overwrite', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/overwrite')) + app.decorateRequest('hooked', '') + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) }) - app.inject({ - url: '/child' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['child'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['child'] }) }) - app.inject({ - url: '/child/grandchild' - }, function (err, res) { - t.error(err) + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject({ url: '/child/grandchild' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['grandchild'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['grandchild'] }) }) - app.inject({ - url: '/sibling' - }, function (err, res) { - t.error(err) + it('should respond correctly to /sibling', async function () { + const res = await app.inject({ url: '/sibling' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root'] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) }) }) diff --git a/test/commonjs/babel-node.js b/test/commonjs/babel-node.js index 11750344..77e459cf 100644 --- a/test/commonjs/babel-node.js +++ b/test/commonjs/babel-node.js @@ -1,34 +1,40 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') const AutoLoad = require('../../') const { join } = require('node:path') -t.plan(7) +describe('Node test suite for babel-node', function () { + const app = Fastify() -const app = Fastify() + before(async function () { + app.register(AutoLoad, { + dir: join(__dirname, 'babel-node/routes') + }) + await app.ready() + }) -app.register(AutoLoad, { - dir: join(__dirname, 'babel-node/routes') -}) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ + url: '/' + }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hello: 'world' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' }) }) - app.inject({ - url: '/foo' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { foo: 'bar' }) + it('should respond correctly to /foo', async function () { + const res = await app.inject({ + url: '/foo' + }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) }) diff --git a/test/commonjs/basic.js b/test/commonjs/basic.js index 88186da3..e23316f0 100644 --- a/test/commonjs/basic.js +++ b/test/commonjs/basic.js @@ -1,291 +1,255 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(107) +describe('Node test suite for basic', function () { + const app = Fastify() -const app = Fastify() - -app.register(require('./basic/app')) + before(async function () { + app.register(require('./basic/app')) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - t.equal(app.foo, 'bar') + it('should set app.foo correctly', function () { + assert.strictEqual(app.foo, 'bar') + }) - app.inject({ - url: '/something' - }, function (err, res) { - t.error(err) + it('should respond correctly to /something', async function () { + const res = await app.inject({ url: '/something' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { something: 'else' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { something: 'else' }) }) - app.inject({ - url: '/autoroute/items/1' - }, function (err, res) { - t.error(err) + it('should respond correctly to /autoroute/items/1', async function () { + const res = await app.inject({ url: '/autoroute/items/1' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) }) - app.inject({ - url: '/autoroute/items' - }, function (err, res) { - t.error(err) + it('should respond correctly to /autoroute/items', async function () { + const res = await app.inject({ url: '/autoroute/items' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) }) - app.inject({ - url: '/autowrap/1' - }, function (err, res) { - t.error(err) + it('should respond correctly to /autowrap/1', async function () { + const res = await app.inject({ url: '/autowrap/1' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) }) - app.inject({ - url: '/autowrap' - }, function (err, res) { - t.error(err) + it('should respond correctly to /autowrap', async function () { + const res = await app.inject({ url: '/autowrap' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) }) - app.inject({ - url: '/semiautomatic/items/1' - }, function (err, res) { - t.error(err) + it('should respond correctly to /semiautomatic/items/1', async function () { + const res = await app.inject({ url: '/semiautomatic/items/1' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) }) - app.inject({ - url: '/semiautomatic/items' - }, function (err, res) { - t.error(err) + it('should respond correctly to /semiautomatic/items', async function () { + const res = await app.inject({ url: '/semiautomatic/items' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) }) - app.inject({ - url: '/bar' - }, function (err, res) { - t.error(err) + it('should respond correctly to /bar', async function () { + const res = await app.inject({ url: '/bar' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { foo: 'bar' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) - app.inject({ - url: '/prefixed' - }, function (err, res) { - t.error(err) + it('should respond correctly to /prefixed', async function () { + const res = await app.inject({ url: '/prefixed' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { something: 'else' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { something: 'else' }) }) - app.inject({ - url: '/default' - }, function (err, res) { - t.error(err) + it('should respond correctly to /default', async function () { + const res = await app.inject({ url: '/default' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { exports: 'default' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { exports: 'default' }) }) - app.inject({ - url: '/skip' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) + it('should respond correctly to /skip', async function () { + const res = await app.inject({ url: '/skip' }) + + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/options' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { - foo: 'bar' - }) + it('should respond correctly to /options', async function () { + const res = await app.inject({ url: '/options' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) - app.inject({ - url: '/defaultPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { index: true }) + it('should respond correctly to /defaultPrefix', async function () { + const res = await app.inject({ url: '/defaultPrefix' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { index: true }) }) - app.inject({ - url: '/defaultPrefix/prefixed' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { prefixed: true }) + it('should respond correctly to /defaultPrefix/prefixed', async function () { + const res = await app.inject({ url: '/defaultPrefix/prefixed' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { prefixed: true }) }) - app.inject({ - url: '/defaultPrefix/overriddenPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) + it('should respond correctly to /defaultPrefix/overriddenPrefix', async function () { + const res = await app.inject({ url: '/defaultPrefix/overriddenPrefix' }) + + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/overriddenPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { overide: 'prefix' }) + it('should respond correctly to /overriddenPrefix', async function () { + const res = await app.inject({ url: '/overriddenPrefix' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { overide: 'prefix' }) }) - app.inject({ - url: '/noPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { no: 'prefix' }) + it('should respond correctly to /noPrefix', async function () { + const res = await app.inject({ url: '/noPrefix' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { no: 'prefix' }) }) - app.inject({ - url: '/a/b/c' - }, function (err, res) { - t.error(err) + it('should respond correctly to /a/b/c', async function () { + const res = await app.inject({ url: '/a/b/c' }) - t.equal(res.statusCode, 200) - t.same(res.payload.toString(), 'd') + assert.strictEqual(res.statusCode, 200) + assert.strictEqual(res.payload.toString(), 'd') }) - app.inject({ - url: '/custom-index/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /custom-index/', async function () { + const res = await app.inject({ url: '/custom-index/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { custom: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { custom: true }) }) - app.inject({ - url: '/index/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /index/', async function () { + const res = await app.inject({ url: '/index/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/index/bar/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /index/bar/', async function () { + const res = await app.inject({ url: '/index/bar/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - ;[ - '/rewrite-route-prefix', - '/rewrite-route-prefix/', - '/rewrite-route-prefix/tre', - '/rewrite-route-prefix/tre/', - '/rewrite-route-prefix/tre/empty/' - ].forEach(url => { - app.inject(url, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200, `OK ${url}`) - t.same(JSON.parse(res.payload), { works: true }, `PAYLOAD ${url}`) - }) + describe('rewrite-route-prefix variants', function () { + const urls = [ + '/rewrite-route-prefix', + '/rewrite-route-prefix/', + '/rewrite-route-prefix/tre', + '/rewrite-route-prefix/tre/', + '/rewrite-route-prefix/tre/empty/' + ] + + for (const url of urls) { + it(`should respond correctly to ${url}`, async function () { + const res = await app.inject({ url }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) + }) + } }) - app.inject({ - url: '/index/ignored' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) + it('should respond correctly to /index/ignored', async function () { + const res = await app.inject({ url: '/index/ignored' }) + + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/one/' - }, function (err, res) { - t.error(err) + it('should respond correctly to /one/', async function () { + const res = await app.inject({ url: '/one/' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/one/two/three' - }, function (err, res) { - t.error(err) + it('should respond correctly to /one/two/three', async function () { + const res = await app.inject({ url: '/one/two/three' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ url: '/routeParams' }, function (err, res) { - t.error(err) + it('should respond correctly to /routeParams', async function () { + const res = await app.inject({ url: '/routeParams' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ url: '/routeParams/foo' }, function (err, res) { - t.error(err) + it('should respond correctly to /routeParams/foo', async function () { + const res = await app.inject({ url: '/routeParams/foo' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ url: '/routeParams/foo/1' }, function (err, res) { - t.error(err) + it('should respond correctly to /routeParams/foo/1', async function () { + const res = await app.inject({ url: '/routeParams/foo/1' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true, id1: '1' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true, id1: '1' }) }) - app.inject({ url: '/routeParams/foo/abc/bar/2' }, function (err, res) { - t.error(err) + it('should respond correctly to /routeParams/foo/abc/bar/2', async function () { + const res = await app.inject({ url: '/routeParams/foo/abc/bar/2' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true, id1: 'abc', id2: '2' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true, id1: 'abc', id2: '2' }) }) - app.inject({ - url: '/encapsulate' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /encapsulate', async function () { + const res = await app.inject({ url: '/encapsulate' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/configPrefix' - }, function (err, res) { - t.error(err) + it('should respond correctly to /configPrefix', async function () { + const res = await app.inject({ url: '/configPrefix' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { configPrefix: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { configPrefix: true }) }) - app.inject({ - url: '/configPrefixCallback' - }, function (err, res) { - t.error(err) + it('should respond correctly to /configPrefixCallback', async function () { + const res = await app.inject({ url: '/configPrefixCallback' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { configPrefixCallback: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { configPrefixCallback: true }) }) }) diff --git a/test/commonjs/cyclic.js b/test/commonjs/cyclic.js index d8418b08..5fa5f074 100644 --- a/test/commonjs/cyclic.js +++ b/test/commonjs/cyclic.js @@ -1,14 +1,21 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(1) +describe('Node test suite for babel-node', function () { + const app = Fastify() -const app = Fastify() + before(async function () { + app.register(require('./cyclic-dependency/app')) + }) -app.register(require('./cyclic-dependency/app')) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.equal(err.message, 'Cyclic dependency') + it('should return cyclic dependency error', async function () { + await assert.rejects(app.ready(), { message: 'Cyclic dependency' }) + }) }) diff --git a/test/commonjs/deep.js b/test/commonjs/deep.js index d9f27d5d..e5008bca 100644 --- a/test/commonjs/deep.js +++ b/test/commonjs/deep.js @@ -1,32 +1,36 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(7) +describe('Node test suite for deep routes', function () { + const app = Fastify() -const app = Fastify() - -app.register(require('./deep/app')) + before(async function () { + app.register(require('./deep/app')) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/with-dirs/level-1/level-2/deep-route' - }, function (err, res) { - t.error(err) + it('should respond correctly to /with-dirs/level-1/level-2/deep-route', async function () { + const res = await app.inject({ + url: '/with-dirs/level-1/level-2/deep-route' + }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'deep-route' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'deep-route' }) }) - app.inject({ - url: '/without-dirs/deep-route' - }, function (err, res) { - t.error(err) + it('should respond correctly to /without-dirs/deep-route', async function () { + const res = await app.inject({ + url: '/without-dirs/deep-route' + }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'deep-route' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'deep-route' }) }) }) diff --git a/test/commonjs/dependency.js b/test/commonjs/dependency.js index 5872033a..44f646a3 100644 --- a/test/commonjs/dependency.js +++ b/test/commonjs/dependency.js @@ -1,70 +1,67 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(22) +describe('Node test suite for dependency', function () { + const app = Fastify() -const app = Fastify() + before(async function () { + app.register(require('./dependency/app')) + await app.ready() + }) -app.register(require('./dependency/app')) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /plugin-a', async function () { + const res = await app.inject({ url: '/plugin-a' }) - app.inject({ - url: '/plugin-a' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-a' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-a' }) }) - app.inject({ - url: '/plugin-b' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-b' }) + it('should respond correctly to /plugin-b', async function () { + const res = await app.inject({ url: '/plugin-b' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-b' }) }) - app.inject({ - url: '/plugin-c' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-c' }) + it('should respond correctly to /plugin-c', async function () { + const res = await app.inject({ url: '/plugin-c' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-c' }) }) - app.inject({ - url: '/plugin-d' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-d' }) + it('should respond correctly to /plugin-d', async function () { + const res = await app.inject({ url: '/plugin-d' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-d' }) }) - app.inject({ - url: '/plugin-e' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-e' }) + it('should respond correctly to /plugin-e', async function () { + const res = await app.inject({ url: '/plugin-e' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-e' }) }) - app.inject({ - url: '/plugin-f' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-f' }) + it('should respond correctly to /plugin-f', async function () { + const res = await app.inject({ url: '/plugin-f' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-f' }) }) - app.inject({ - url: '/plugin-g' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload).path, '/plugin-g') + it('should respond correctly to /plugin-g', async function () { + const res = await app.inject({ url: '/plugin-g' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload).path, '/plugin-g') }) }) diff --git a/test/commonjs/error.js b/test/commonjs/error.js index 7070003a..43dc8a35 100644 --- a/test/commonjs/error.js +++ b/test/commonjs/error.js @@ -1,50 +1,77 @@ 'use strict' -const t = require('tap') -const fastify = require('fastify') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') +const Fastify = require('fastify') + const runtime = require('../../lib/runtime') const typeStrippingEnabled = runtime.nodeVersion >= 23 -t.test('independent of module support', function (t) { - t.plan(typeStrippingEnabled ? 7 : 8) - const app = fastify() +describe('independent module support - unexpected token', function () { + const app = Fastify() + + before(async function () { + app.register(require('./syntax-error/app')) + }) - app.register(require('./syntax-error/app')) + after(async function () { + await app.close() + }) - app.ready(function (err) { - t.type(err, SyntaxError) - t.match(err.message, /unexpected token/i) + it('should return unexpected token error', async function () { + await assert.rejects(app.ready(), SyntaxError, /unexpected token/i) }) +}) + +describe('independent module support - cannot import plugin index', function () { + const app = Fastify() - const app2 = fastify() + before(async function () { + app.register(require('./index-error/app')) + }) - app2.register(require('./index-error/app')) + after(async function () { + await app.close() + }) - app2.ready(function (err) { - t.type(err, Error) - t.match(err.message, /cannot import plugin.*index/i) + it('should return cannot import plugin index error', async function () { + await assert.rejects(app.ready(), Error, /cannot import plugin.*index/i) }) +}) + +describe('independent module support - cannot import plugin typescript', function () { + const app = Fastify() - const app3 = fastify() + before(async function () { + app.register(require('./ts-error/app')) + }) - app3.register(require('./ts-error/app')) + after(async function () { + await app.close() + }) - app3.ready(function (err) { + it('should return cannot import plugin typescript error', async function () { if (typeStrippingEnabled) { - t.error(err) + assert.doesNotThrow(() => app.ready()) } else { - t.type(err, Error) - t.match(err.message, /cannot import plugin.*typescript/i) + await assert.rejects(app.ready(), Error, /cannot import plugin.*typescript/i) } }) +}) + +describe('independent module support - cyclic dependency error', function () { + const app = Fastify() - const app4 = fastify() + before(async function () { + app.register(require('./cyclic-dependency/app')) + }) - app4.register(require('./cyclic-dependency/app')) + after(async function () { + await app.close() + }) - app4.ready(function (err) { - t.type(err, Error) - t.equal(err.message, 'Cyclic dependency') + it('should return cannot import plugin typescript error', async function () { + await assert.rejects(app.ready(), new Error('Cyclic dependency')) }) }) diff --git a/test/commonjs/graph-dependency.js b/test/commonjs/graph-dependency.js index 0cabf150..58bcac28 100644 --- a/test/commonjs/graph-dependency.js +++ b/test/commonjs/graph-dependency.js @@ -1,42 +1,42 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(9) +describe('Node test suite for graph dependency', function () { + const app = Fastify() -const app = Fastify() + before(async function () { + app.register(require('./graph-dependency/app')) + await app.ready() + }) -app.register(require('./graph-dependency/app')) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /a', async function () { + const res = await app.inject({ url: '/a' }) - app.inject({ - url: '/a' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) + assert.strictEqual(res.statusCode, 200) }) - app.inject({ - url: '/b' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) + it('should respond correctly to /b', async function () { + const res = await app.inject({ url: '/b' }) + + assert.strictEqual(res.statusCode, 200) }) - app.inject({ - url: '/c' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) + it('should respond correctly to /c', async function () { + const res = await app.inject({ url: '/c' }) + + assert.strictEqual(res.statusCode, 200) }) - app.inject({ - url: '/d' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) + it('should respond correctly to /d', async function () { + const res = await app.inject({ url: '/d' }) + + assert.strictEqual(res.statusCode, 200) }) }) diff --git a/test/commonjs/index-package.js b/test/commonjs/index-package.js index 46144fdc..579e77df 100644 --- a/test/commonjs/index-package.js +++ b/test/commonjs/index-package.js @@ -1,22 +1,25 @@ 'use strict' -const t = require('tap') -const fastify = require('fastify') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') +const Fastify = require('fastify') -t.plan(4) +describe('Node test suite for index package', function () { + const app = Fastify() -const app = fastify() + before(async function () { + app.register(require('./index-package/app')) + await app.ready() + }) -app.register(require('./index-package/app')) + after(async function () { + await app.close() + }) -app.ready(err => { - t.error(err) + it('should respond correctly to /foo/bar', async function () { + const res = await app.inject({ url: '/foo/bar' }) - app.inject({ - url: '/foo/bar' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { success: true }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { success: true }) }) }) diff --git a/test/commonjs/non-plugin.js b/test/commonjs/non-plugin.js index 6b5952fc..08e9ead2 100644 --- a/test/commonjs/non-plugin.js +++ b/test/commonjs/non-plugin.js @@ -3,24 +3,26 @@ // This test tests that automatic loading will skip modules that do not // export a function, i.e. not a fastify plugin. -const t = require('tap') -const fastify = require('fastify') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') +const Fastify = require('fastify') -t.plan(4) +describe('Node test suite for non-plugin', function () { + const app = Fastify() -const app = fastify() - -app.register(require('./non-plugin/app')) + before(async function () { + app.register(require('./non-plugin/app')) + await app.ready() + }) -app.ready(err => { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/foo' - }, function (err, res) { - t.error(err) + it('should respond correctly to /foo', async function () { + const res = await app.inject({ url: '/foo' }) - t.equal(res.statusCode, 200) - t.same(res.payload, 'foo') + assert.strictEqual(res.statusCode, 200) + assert.strictEqual(res.payload, 'foo') }) }) diff --git a/test/commonjs/options.js b/test/commonjs/options.js index ea8a574d..bbfbda9f 100644 --- a/test/commonjs/options.js +++ b/test/commonjs/options.js @@ -1,73 +1,68 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(22) +describe('Node test suite for options', function () { + const app = Fastify() -const app = Fastify() - -app.decorate('root', 'root') + before(async function () { + app.decorate('root', 'root') + app.register(require('./options/app')) + await app.ready() + }) -app.register(require('./options/app')) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /plugin-a with global option override', async function () { + const res = await app.inject({ url: '/plugin-a' }) - app.inject({ - url: '/plugin-a' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - // a is overriden by global option: - t.same(JSON.parse(res.payload), { data: 'test-1' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-1' }) }) - app.inject({ - url: '/plugin-b' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'override' }) + it('should respond correctly to /plugin-b', async function () { + const res = await app.inject({ url: '/plugin-b' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'override' }) }) - app.inject({ - url: '/plugin-default' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'default' }) + it('should respond correctly to /plugin-default', async function () { + const res = await app.inject({ url: '/plugin-default' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'default' }) }) - app.inject({ - url: '/plugin-c' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'c' }) + it('should respond correctly to /plugin-c', async function () { + const res = await app.inject({ url: '/plugin-c' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'c' }) }) - app.inject({ - url: '/plugin-d' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'test-3' }) + it('should respond correctly to /plugin-d', async function () { + const res = await app.inject({ url: '/plugin-d' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-3' }) }) - app.inject({ - url: '/plugin-e' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'test-4-root' }) + it('should respond correctly to /plugin-e', async function () { + const res = await app.inject({ url: '/plugin-e' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-4-root' }) }) - app.inject({ - url: '/plugin-y' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'y' }) + it('should respond correctly to /plugin-y', async function () { + const res = await app.inject({ url: '/plugin-y' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'y' }) }) }) diff --git a/test/commonjs/route-parameters-basic.js b/test/commonjs/route-parameters-basic.js index c954e4ea..7797057d 100644 --- a/test/commonjs/route-parameters-basic.js +++ b/test/commonjs/route-parameters-basic.js @@ -1,50 +1,55 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(13) +describe('Node test suite for route parameters basic', function () { + const app = Fastify() -const app = Fastify() - -app.register(require('./route-parameters/basic')) + before(async function () { + app.register(require('./route-parameters/basic')) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/users' - }, function (err, res) { - t.error(err) + it('should respond correctly to /users', async function () { + const res = await app.inject({ url: '/users' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { users: [{ id: 7, username: 'example' }] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + users: [{ id: 7, username: 'example' }], + }) }) - app.inject({ - url: '/users/7' - }, function (err, res) { - t.error(err) + it('should respond correctly to /users/7', async function () { + const res = await app.inject({ url: '/users/7' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { user: { id: 7, username: 'example' } }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + user: { id: '7', username: 'example' }, + }) }) - app.inject({ - url: '/users/_id' - }, function (err, res) { - t.error(err) + it('should respond correctly to /users/_id', async function () { + const res = await app.inject({ url: '/users/_id' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { user: { id: '_id', username: 'example' } }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + user: { id: '_id', username: 'example' }, + }) }) - app.inject({ - url: '/be-nl' - }, function (err, res) { - t.error(err) + it('should respond correctly to /be-nl', async function () { + const res = await app.inject({ url: '/be-nl' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { country: 'be', language: 'nl' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + country: 'be', + language: 'nl', + }) }) }) diff --git a/test/commonjs/route-parameters-disabled.js b/test/commonjs/route-parameters-disabled.js index e94b9a67..6fd7f85e 100644 --- a/test/commonjs/route-parameters-disabled.js +++ b/test/commonjs/route-parameters-disabled.js @@ -1,40 +1,38 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') -t.plan(9) +describe('Node test suite for route parameters disabled', function () { + const app = Fastify() -const app = Fastify() - -app.register(require('./route-parameters/disabled')) + before(async function () { + app.register(require('./route-parameters/disabled')) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/users' - }, function (err, res) { - t.error(err) + it('should respond correctly to /users', async function () { + const res = await app.inject({ url: '/users' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { users: [{ id: 7, username: 'example' }] }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { users: [{ id: 7, username: 'example' }] }) }) - app.inject({ - url: '/users/7' - }, function (err, res) { - t.error(err) + it('should respond with 404 to /users/7', async function () { + const res = await app.inject({ url: '/users/7' }) - t.equal(res.statusCode, 404) + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/users/_id' - }, function (err, res) { - t.error(err) + it('should respond correctly to /users/_id', async function () { + const res = await app.inject({ url: '/users/_id' }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { user: { id: 'null', username: 'example' } }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { user: { id: 'null', username: 'example' } }) }) }) diff --git a/test/issues/369/test.js b/test/issues/369/test.js index 5bc9cfa7..4beef2dc 100644 --- a/test/issues/369/test.js +++ b/test/issues/369/test.js @@ -1,188 +1,161 @@ 'use strict' -const { test } = require('tap') +const { describe, it } = require('node:test') +const assert = require('node:assert') const Fastify = require('fastify') const path = require('node:path') const autoload = require('../../..') const runtime = require('../../../lib/runtime') -test('Should throw an error when trying to load invalid hooks', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'invalid-autohooks'), - autoHooks: true +describe('Issue 369 tests', async function () { + it('Should throw an error when trying to load invalid hooks', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'invalid-autohooks'), + autoHooks: true, + }) + + await assert.rejects(app.ready(), /Invalid or unexpected token/) + }) + + it('Should throw an error when trying to import hooks plugin using index.ts if typescriptSupport is not enabled', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'invalid-index-type'), + autoHooks: true, + }) + if (runtime.nodeVersion >= 23) { + assert.doesNotThrow(() => app.ready()) + } else { + await assert.rejects( + app.ready(), + new Error( + `@fastify/autoload cannot import hooks plugin at '${path.join( + __dirname, + 'invalid-index-type/index.ts' + )}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.` + ) + ) + } + }) + + it("Should not accumulate plugin if doesn't comply to matchFilter", async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'routes'), + }) + + await app.ready() + const res = await app.inject({ url: '/' }) + assert.strictEqual(res.statusCode, 200) + + const app2 = Fastify() + app2.register(autoload, { + dir: path.join(__dirname, 'routes'), + matchFilter: /invalid/, + }) + + await app2.ready() + const res2 = await app2.inject({ url: '/' }) + assert.strictEqual(res2.statusCode, 404) + }) + + it('Should be able to filter paths using a string', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'routes'), + matchFilter: 'routes.js', + }) + + await app.ready() + const res = await app.inject({ url: '/' }) + assert.strictEqual(res.statusCode, 200) + + const app2 = Fastify() + app2.register(autoload, { + dir: path.join(__dirname, 'routes'), + matchFilter: 'invalid-path', + }) + + await app2.ready() + const res2 = await app2.inject({ url: '/' }) + assert.strictEqual(res2.statusCode, 404) + }) + + it('Should be able to filter paths using a function', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'routes'), + matchFilter: (path) => path.includes('routes.js'), + }) + + await app.ready() + const res = await app.inject({ url: '/' }) + assert.strictEqual(res.statusCode, 200) + + const app2 = Fastify() + app2.register(autoload, { + dir: path.join(__dirname, 'routes'), + matchFilter: (path) => path.includes('invalid-path'), + }) + + await app2.ready() + const res2 = await app2.inject({ url: '/' }) + assert.strictEqual(res2.statusCode, 404) + }) + + it('Should not accumulate plugin if ignoreFilter is matched', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'routes'), + ignoreFilter: /\/not-exists.js/, + }) + + await app.ready() + const res = await app.inject({ url: '/' }) + assert.strictEqual(res.statusCode, 200) + + const app2 = Fastify() + app2.register(autoload, { + dir: path.join(__dirname, 'routes'), + ignoreFilter: /\/routes.js/, + autoHooks: true, + }) + + await app2.ready() + const res2 = await app2.inject({ url: '/' }) + assert.strictEqual(res2.statusCode, 404) + }) + + it('Should not set skip-override if hook plugin is not a function or async function', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'routes'), + autoHooks: true, + cascadeHooks: true, + }) + + app.decorateRequest('hooked', '') + await app.ready() + + const res = await app.inject({ url: '/child' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + hooked: ['root', 'child'], + }) + + const res2 = await app.inject({ url: '/promisified' }) + assert.strictEqual(res2.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res2.payload), { hooked: ['root'] }) + }) + + it('Should not enrich non-SyntaxError', async function () { + const app = Fastify() + app.register(autoload, { + dir: path.join(__dirname, 'non-SyntaxError'), + autoHooks: true, + }) + + await assert.rejects(app.ready(), new ReferenceError('x is not defined')) }) - - await t.rejects(app.ready(), /Invalid or unexpected token/) -}) - -test('Should throw an error when trying to import hooks plugin using index.ts if typescriptSupport is not enabled', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'invalid-index-type'), - autoHooks: true - }) - - if (runtime.nodeVersion >= 23) { - t.doesNotThrow(() => app.ready()) - } else { - await t.rejects(app.ready(), new Error(`@fastify/autoload cannot import hooks plugin at '${path.join(__dirname, 'invalid-index-type/index.ts')}'`)) - } -}) - -test('Should not accumulate plugin if doesn\'t comply to matchFilter', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'routes') - }) - - await app.ready() - - const res = await app.inject({ - url: '/' - }) - - t.equal(res.statusCode, 200) - - const app2 = Fastify() - app2.register(autoload, { - dir: path.join(__dirname, 'routes'), - matchFilter: /invalid/ - }) - - await app2.ready() - - const res2 = await app2.inject({ - url: '/' - }) - - t.equal(res2.statusCode, 404) -}) - -test('Should be able to filter paths using a string', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'routes'), - matchFilter: 'routes.js' - }) - - await app.ready() - - const res = await app.inject({ - url: '/' - }) - - t.equal(res.statusCode, 200) - - const app2 = Fastify() - app2.register(autoload, { - dir: path.join(__dirname, 'routes'), - matchFilter: 'invalid-path' - }) - - await app2.ready() - - const res2 = await app2.inject({ - url: '/' - }) - - t.equal(res2.statusCode, 404) -}) - -test('Should be able to filter paths using a function', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'routes'), - matchFilter: (path) => path.includes('routes.js') - }) - - await app.ready() - - const res = await app.inject({ - url: '/' - }) - - t.equal(res.statusCode, 200) - - const app2 = Fastify() - app2.register(autoload, { - dir: path.join(__dirname, 'routes'), - matchFilter: (path) => path.includes('invalid-path') - }) - - await app2.ready() - - const res2 = await app2.inject({ - url: '/' - }) - - t.equal(res2.statusCode, 404) -}) - -test('Should not accumulate plugin if ignoreFilter is matched', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'routes'), - ignoreFilter: /\/not-exists.js/ - }) - - await app.ready() - - const res = await app.inject({ - url: '/' - }) - - t.equal(res.statusCode, 200) - - const app2 = Fastify() - app2.register(autoload, { - dir: path.join(__dirname, 'routes'), - ignoreFilter: /\/routes.js/, - autoHooks: true - }) - - await app2.ready() - - const res2 = await app2.inject({ - url: '/' - }) - - t.equal(res2.statusCode, 404) -}) - -test('Should not set skip-override if hook plugin is not a function or async function', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - cascadeHooks: true - }) - - app.decorateRequest('hooked', '') - - await app.ready() - - const res = await app.inject({ - url: '/child' - }) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { hooked: ['root', 'child'] }) - - const res2 = await app.inject({ - url: '/promisified' - }) - - t.equal(res2.statusCode, 200) - t.same(JSON.parse(res2.payload), { hooked: ['root'] }) -}) - -test('Should not enrich non-SyntaxError', async (t) => { - const app = Fastify() - app.register(autoload, { - dir: path.join(__dirname, 'non-SyntaxError'), - autoHooks: true - }) - - t.rejects(app.ready(), new ReferenceError('x is not defined')) }) diff --git a/test/issues/374/test.js b/test/issues/374/test.js index ce1d05c0..de0d38ad 100644 --- a/test/issues/374/test.js +++ b/test/issues/374/test.js @@ -1,45 +1,50 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const path = require('node:path') const Fastify = require('fastify') const autoLoad = require('../../../') -t.plan(10) - -const app = Fastify() +describe('Issue 374 tests', function () { + const app = Fastify() + + before(async function () { + app.register(autoLoad, { + dir: path.join(__dirname, 'routes'), + autoHooks: true, + cascadeHooks: true, + routeParams: true, + }) + await app.ready() + }) -app.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - cascadeHooks: true, - routeParams: true -}) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { path: '/' }) }) - app.inject({ - url: '/entity' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/entity', hooked: ['root'] }) + it('should respond correctly to /entity', async function () { + const res = await app.inject({ url: '/entity' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { path: '/entity', hooked: ['root'] }) }) - app.inject({ - url: '/entity/1' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/entity/1', hooked: ['root'], params: { entity: 1 } }) + it('should respond correctly to /entity/1', async function () { + const res = await app.inject({ url: '/entity/1' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + path: '/entity/1', + hooked: ['root'], + params: { entity: '1' }, + }) }) }) diff --git a/test/issues/376/test.js b/test/issues/376/test.js index d1f42b2d..96980971 100644 --- a/test/issues/376/test.js +++ b/test/issues/376/test.js @@ -1,58 +1,64 @@ 'use strict' -const t = require('tap') +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') const path = require('node:path') const Fastify = require('fastify') const autoLoad = require('../../../') -t.plan(13) +describe('Issue 376 tests', function () { + const app = Fastify() -const app = Fastify() + before(async function () { + app.register(autoLoad, { + dir: path.join(__dirname, 'routes'), + autoHooks: true, + options: { prefix: '/api' }, + }) -app.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - options: { prefix: '/api' } -}) + app.register(autoLoad, { + dir: path.join(__dirname, 'routes'), + autoHooks: true, + options: { prefix: '/prefix/' }, + }) + await app.ready() + }) -app.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - options: { prefix: '/prefix/' } -}) + after(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond correctly to /api', async function () { + const res = await app.inject({ url: '/api' }) - app.inject({ - url: '/api' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/api' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { path: '/api' }) }) - app.inject({ - url: '/api/entity' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/api/entity', hooked: ['root'] }) + it('should respond correctly to /api/entity', async function () { + const res = await app.inject({ url: '/api/entity' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + path: '/api/entity', + hooked: ['root'], + }) }) - app.inject({ - url: '/prefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/prefix' }) + it('should respond correctly to /prefix', async function () { + const res = await app.inject({ url: '/prefix' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { path: '/prefix' }) }) - app.inject({ - url: '/prefix/entity' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/prefix/entity', hooked: ['root'] }) + it('should respond correctly to /prefix/entity', async function () { + const res = await app.inject({ url: '/prefix/entity' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + path: '/prefix/entity', + hooked: ['root'], + }) }) }) diff --git a/test/issues/388/test.js b/test/issues/388/test.js index ed4b55a5..64565bbb 100644 --- a/test/issues/388/test.js +++ b/test/issues/388/test.js @@ -1,44 +1,45 @@ 'use strict' -const t = require('tap') +const { afterEach, beforeEach, describe, it } = require('node:test') +const assert = require('node:assert') const path = require('node:path') const Fastify = require('fastify') const autoLoad = require('../../../') -t.plan(7) +describe('Issue 388 tests', function () { + let app -const app = Fastify() + beforeEach(async function () { + app = Fastify() + }) -app.register(autoLoad, { - dir: path.join(__dirname, 'routes') -}) + afterEach(async function () { + await app.close() + }) -app.ready(function (err) { - t.error(err) + it('should respond 404 to /', async function () { + app = Fastify() + app.register(autoLoad, { + dir: path.join(__dirname, 'routes'), + }) + await app.ready() - app.inject({ - url: '/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) - }) -}) + const res = await app.inject({ url: '/' }) -const app2 = Fastify() + assert.strictEqual(res.statusCode, 404) + }) -app2.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - scriptPattern: /(js|ts|tsx)$/ -}) + it('should respond 200 to / when script pattern is loaded', async function () { + app = Fastify() + app.register(autoLoad, { + dir: path.join(__dirname, 'routes'), + scriptPattern: /(js|ts|tsx)$/, + }) + await app.ready() -app2.ready(function (err) { - t.error(err) + const res = await app.inject({ url: '/' }) - app2.inject({ - url: '/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { tsx: 'ok' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { tsx: 'ok' }) }) }) diff --git a/test/module/autohooks.js b/test/module/autohooks.js index ba7a8db0..7c20455d 100644 --- a/test/module/autohooks.js +++ b/test/module/autohooks.js @@ -1,112 +1,161 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import autoHooksBasic from './autohooks/basic.mjs' import autoHooksCascade from './autohooks/cascade.mjs' import autoHooksOverwrite from './autohooks/overwrite.mjs' import autoHooksDisabled from './autohooks/disabled.mjs' -const { test } = t - -test('autohooks: Default behaviour', async () => { +describe('autohooks: Default behaviour', function () { const app = fastify() - let res - - app.register(autoHooksBasic) - app.decorateRequest('hooked', '') - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root'] }) - - res = await app.inject('/child') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['child'] }) - - res = await app.inject('/child/grandchild') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['grandchild'] }) - res = await app.inject('/sibling') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: '' }) + before(async function () { + app.register(autoHooksBasic) + app.decorateRequest('hooked', '') + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root'] }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject('/child') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['child'] }) + }) + + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject('/child/grandchild') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['grandchild'] }) + }) + + it('should respond correctly to /sibling', async function () { + const res = await app.inject('/sibling') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: '' }) + }) }) -test('autohooks: Cascade behaviour', async () => { +describe('autohooks: Cascade behaviour', function () { const app = fastify() - let res - - app.register(autoHooksCascade) - app.decorateRequest('hooked', '') - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root'] }) - - res = await app.inject('/child') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root', 'child'] }) - - res = await app.inject('/child/grandchild') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root', 'child', 'grandchild'] }) - res = await app.inject('/sibling') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root'] }) + before(async function () { + app.register(autoHooksCascade) + app.decorateRequest('hooked', '') + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root'] }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject('/child') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root', 'child'] }) + }) + + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject('/child/grandchild') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + hooked: ['root', 'child', 'grandchild'], + }) + }) + + it('should respond correctly to /sibling', async function () { + const res = await app.inject('/sibling') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root'] }) + }) }) -test('autohooks: Overwrite cascade behaviour', async () => { +describe('autohooks: Overwrite cascade behaviour', function () { const app = fastify() - let res - app.register(autoHooksOverwrite) - app.decorateRequest('hooked', '') - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root'] }) - - res = await app.inject('/child') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['child'] }) - - res = await app.inject('/child/grandchild') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['grandchild'] }) - - res = await app.inject('/sibling') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: ['root'] }) + before(async function () { + app.register(autoHooksOverwrite) + app.decorateRequest('hooked', '') + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root'] }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject('/child') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['child'] }) + }) + + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject('/child/grandchild') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['grandchild'] }) + }) + + it('should respond correctly to /sibling', async function () { + const res = await app.inject('/sibling') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: ['root'] }) + }) }) -test('autohooks: Disabled behaviour', async () => { +describe('autohooks: Disabled behaviour', function () { const app = fastify() - let res - - app.register(autoHooksDisabled) - app.decorateRequest('hooked', 'disabled') - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: 'disabled' }) - - res = await app.inject('/child') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: 'disabled' }) - - res = await app.inject('/child/grandchild') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: 'disabled' }) - res = await app.inject('/sibling') - t.equal(res.statusCode, 200) - t.same(res.json(), { hooked: 'disabled' }) + before(async function () { + app.register(autoHooksDisabled) + app.decorateRequest('hooked', 'disabled') + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: 'disabled' }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject('/child') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: 'disabled' }) + }) + + it('should respond correctly to /child/grandchild', async function () { + const res = await app.inject('/child/grandchild') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: 'disabled' }) + }) + + it('should respond correctly to /sibling', async function () { + const res = await app.inject('/sibling') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { hooked: 'disabled' }) + }) }) diff --git a/test/module/basic.js b/test/module/basic.js index 9aa91e77..9ae1e010 100644 --- a/test/module/basic.js +++ b/test/module/basic.js @@ -1,248 +1,192 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import basicApp from './basic/app.js' -t.plan(80) +describe('basic tests', function () { + const app = fastify() -const app = fastify() - -app.register(basicApp) - -app.ready(function (err) { - t.error(err) - - app.inject({ - url: '/something' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { something: 'else' }) + before(async function () { + app.register(basicApp) + await app.ready() }) - app.inject({ - url: '/autoroute/items/1' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + after(async function () { + await app.close() }) - app.inject({ - url: '/autoroute/items' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + it('should respond correctly to /something', async function () { + const res = await app.inject({ url: '/something' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { something: 'else' }) }) - app.inject({ - url: '/autowrap/1' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + it('should respond correctly to /autoroute/items/1', async function () { + const res = await app.inject({ url: '/autoroute/items/1' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) }) - app.inject({ - url: '/autowrap' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + it('should respond correctly to /autoroute/items', async function () { + const res = await app.inject({ url: '/autoroute/items' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [ + { answer: 42 }, + { answer: 41 }, + ]) }) - app.inject({ - url: '/semiautomatic/items/1' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { answer: 42 }) + it('should respond correctly to /autowrap/1', async function () { + const res = await app.inject({ url: '/autowrap/1' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) }) - app.inject({ - url: '/semiautomatic/items' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), [{ answer: 42 }, { answer: 41 }]) + it('should respond correctly to /autowrap', async function () { + const res = await app.inject({ url: '/autowrap' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [ + { answer: 42 }, + { answer: 41 }, + ]) }) - app.inject({ - url: '/bar' - }, function (err, res) { - t.error(err) + it('should respond correctly to /semiautomatic/items/1', async function () { + const res = await app.inject({ url: '/semiautomatic/items/1' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { answer: 42 }) + }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { foo: 'bar' }) + it('should respond correctly to /semiautomatic/items', async function () { + const res = await app.inject({ url: '/semiautomatic/items' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), [ + { answer: 42 }, + { answer: 41 }, + ]) }) - app.inject({ - url: '/prefixed' - }, function (err, res) { - t.error(err) + it('should respond correctly to /bar', async function () { + const res = await app.inject({ url: '/bar' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) + }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { something: 'else' }) + it('should respond correctly to /prefixed', async function () { + const res = await app.inject({ url: '/prefixed' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { something: 'else' }) }) - app.inject({ - url: '/skip' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) + it('should respond correctly to /skip', async function () { + const res = await app.inject({ url: '/skip' }) + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/options' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { - foo: 'bar' - }) + it('should respond correctly to /options', async function () { + const res = await app.inject({ url: '/options' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) - app.inject({ - url: '/commonjs' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { - foo: 'bar' - }) + it('should respond correctly to /commonjs', async function () { + const res = await app.inject({ url: '/commonjs' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) - app.inject({ - url: '/module' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { - foo: 'bar' - }) + it('should respond correctly to /module', async function () { + const res = await app.inject({ url: '/module' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { foo: 'bar' }) }) - app.inject({ - url: '/defaultPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { index: true }) + it('should respond correctly to /defaultPrefix', async function () { + const res = await app.inject({ url: '/defaultPrefix' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { index: true }) }) - app.inject({ - url: '/defaultPrefix/prefixed' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { prefixed: true }) + it('should respond correctly to /defaultPrefix/prefixed', async function () { + const res = await app.inject({ url: '/defaultPrefix/prefixed' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { prefixed: true }) }) - app.inject({ - url: '/defaultPrefix/overriddenPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) + it('should respond correctly to /defaultPrefix/overriddenPrefix', async function () { + const res = await app.inject({ url: '/defaultPrefix/overriddenPrefix' }) + assert.strictEqual(res.statusCode, 404) }) - app.inject({ - url: '/overriddenPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { overide: 'prefix' }) + it('should respond correctly to /overriddenPrefix', async function () { + const res = await app.inject({ url: '/overriddenPrefix' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { overide: 'prefix' }) }) - app.inject({ - url: '/noPrefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { no: 'prefix' }) + it('should respond correctly to /noPrefix', async function () { + const res = await app.inject({ url: '/noPrefix' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { no: 'prefix' }) }) - app.inject({ - url: '/one/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /one/', async function () { + const res = await app.inject({ url: '/one/' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/one/two/three' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /one/two/three', async function () { + const res = await app.inject({ url: '/one/two/three' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/ten/' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /ten/', async function () { + const res = await app.inject({ url: '/ten/' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/ten/eight' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /ten/eight', async function () { + const res = await app.inject({ url: '/ten/eight' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/nested/shallow' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /nested/shallow', async function () { + const res = await app.inject({ url: '/nested/shallow' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/nested/shallow/deep' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { works: true }) + it('should respond correctly to /nested/shallow/deep', async function () { + const res = await app.inject({ url: '/nested/shallow/deep' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { works: true }) }) - app.inject({ - url: '/nested/shallow/deep/deeper' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 404) - t.same(JSON.parse(res.payload), { + it('should respond correctly to /nested/shallow/deep/deeper', async function () { + const res = await app.inject({ url: '/nested/shallow/deep/deeper' }) + assert.strictEqual(res.statusCode, 404) + assert.deepStrictEqual(JSON.parse(res.payload), { message: 'Route GET:/nested/shallow/deep/deeper not found', error: 'Not Found', - statusCode: 404 + statusCode: 404, }) }) - app.inject({ - url: '/configPrefix' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { configPrefix: true }) + it('should respond correctly to /configPrefix', async function () { + const res = await app.inject({ url: '/configPrefix' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { configPrefix: true }) }) - app.inject({ - url: '/configPrefixCallback' - }, function (err, res) { - t.error(err) - - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { configPrefixCallback: true }) + it('should respond correctly to /configPrefixCallback', async function () { + const res = await app.inject({ url: '/configPrefixCallback' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { + configPrefixCallback: true, + }) }) }) diff --git a/test/module/dependency.js b/test/module/dependency.js index 08c60942..424510eb 100644 --- a/test/module/dependency.js +++ b/test/module/dependency.js @@ -1,69 +1,59 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import dependencyApp from './dependency/app.js' -t.plan(22) +describe('dependency tests', function () { + const app = fastify() -const app = fastify() - -app.register(dependencyApp) + before(async function () { + app.register(dependencyApp) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/plugin-a' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-a' }) + it('should respond correctly to /plugin-a', async function () { + const res = await app.inject({ url: '/plugin-a' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-a' }) }) - app.inject({ - url: '/plugin-b' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-b' }) + it('should respond correctly to /plugin-b', async function () { + const res = await app.inject({ url: '/plugin-b' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-b' }) }) - app.inject({ - url: '/plugin-c' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-c' }) + it('should respond correctly to /plugin-c', async function () { + const res = await app.inject({ url: '/plugin-c' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-c' }) }) - app.inject({ - url: '/plugin-d' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-d' }) + it('should respond correctly to /plugin-d', async function () { + const res = await app.inject({ url: '/plugin-d' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-d' }) }) - app.inject({ - url: '/plugin-e' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-e' }) + it('should respond correctly to /plugin-e', async function () { + const res = await app.inject({ url: '/plugin-e' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-e' }) }) - app.inject({ - url: '/plugin-f' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'plugin-f' }) + it('should respond correctly to /plugin-f', async function () { + const res = await app.inject({ url: '/plugin-f' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'plugin-f' }) }) - app.inject({ - url: '/plugin-g' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload).path, '/plugin-g') + it('should respond correctly to /plugin-g', async function () { + const res = await app.inject({ url: '/plugin-g' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload).path, '/plugin-g') }) }) diff --git a/test/module/esm-import.js b/test/module/esm-import.js index b5526862..38685384 100644 --- a/test/module/esm-import.js +++ b/test/module/esm-import.js @@ -1,59 +1,57 @@ -import t from 'tap' +import { afterEach, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import esmImportAppDefault from './esm-import/app-default.js' import esmImportAppNamed from './esm-import/app-named.js' import esmImportAppStarDefault from './esm-import/app-star-default.js' import esmImportAppStarNamed from './esm-import/app-star-named.js' -const { test } = t - -test('default', async () => { - const app = fastify() - - app.register(esmImportAppDefault) - - await app.ready() - - const res = await app.inject('/default') - t.equal(res.statusCode, 200) - t.same(res.json(), { script: 'default' }) -}) - -test('named', async () => { - const app = fastify() - - app.register(esmImportAppNamed) - - await app.ready() - - const res = await app.inject('/named') - - t.equal(res.statusCode, 200) - t.same(res.json(), { script: 'named' }) -}) - -test('star-default', async () => { - const app = fastify() - - app.register(esmImportAppStarDefault) - - await app.ready() - - const res = await app.inject('/star-default') - - t.equal(res.statusCode, 200) - t.same(res.json(), { script: 'star-default' }) -}) - -test('star-named', async () => { - const app = fastify() - - app.register(esmImportAppStarNamed) - - await app.ready() - - const res = await app.inject('/star-named') - - t.equal(res.statusCode, 200) - t.same(res.json(), { script: 'star-named' }) +describe('ESM import tests', async function () { + let app + afterEach(async () => { + await app.close() + }) + + it('default', async function () { + app = fastify() + app.register(esmImportAppDefault) + await app.ready() + + const res = await app.inject('/default') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { script: 'default' }) + await app.close() + }) + + it('named', async function () { + app = fastify() + app.register(esmImportAppNamed) + await app.ready() + + const res = await app.inject('/named') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { script: 'named' }) + await app.close() + }) + + it('star default', async function () { + app = fastify() + app.register(esmImportAppStarDefault) + await app.ready() + + const res = await app.inject('/star-default') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { script: 'star-default' }) + await app.close() + }) + + it('star named', async function () { + app = fastify() + app.register(esmImportAppStarNamed) + await app.ready() + + const res = await app.inject('/star-named') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { script: 'star-named' }) + }) }) diff --git a/test/module/index-package.js b/test/module/index-package.js index 1b02aa8e..874aea79 100644 --- a/test/module/index-package.js +++ b/test/module/index-package.js @@ -1,14 +1,24 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import indexPackage from './index-package/app.js' -t.test('index package', async () => { +describe('index package', function () { const app = fastify() - app.register(indexPackage) + before(async function () { + app.register(indexPackage) + await app.ready() + }) - const res = await app.inject('/foo/bar') + after(async function () { + await app.close() + }) - t.equal(res.statusCode, 200) - t.same(res.json(), { success: true }) + it('should handle foo/bar correctly', async function () { + const res = await app.inject('/foo/bar') + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { success: true }) + }) }) diff --git a/test/module/options.js b/test/module/options.js index 7dcf5a57..2ddebfd5 100644 --- a/test/module/options.js +++ b/test/module/options.js @@ -1,72 +1,60 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import optionsApp from './options/app.js' -t.plan(22) +describe('options tests', function () { + const app = fastify() -const app = fastify() - -app.decorate('root', 'root') - -app.register(optionsApp) + before(async function () { + app.decorate('root', 'root') + app.register(optionsApp) + await app.ready() + }) -app.ready(function (err) { - t.error(err) + after(async function () { + await app.close() + }) - app.inject({ - url: '/plugin-a' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - // a is overriden by global option: - t.same(JSON.parse(res.payload), { data: 'test-1' }) + it('should respond correctly to /plugin-a', async function () { + const res = await app.inject({ url: '/plugin-a' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-1' }) }) - app.inject({ - url: '/plugin-b' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'override' }) + it('should respond correctly to /plugin-b', async function () { + const res = await app.inject({ url: '/plugin-b' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'override' }) }) - app.inject({ - url: '/plugin-default' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'default' }) + it('should respond correctly to /plugin-default', async function () { + const res = await app.inject({ url: '/plugin-default' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'default' }) }) - app.inject({ - url: '/plugin-c' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'c' }) + it('should respond correctly to /plugin-c', async function () { + const res = await app.inject({ url: '/plugin-c' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'c' }) }) - app.inject({ - url: '/plugin-d' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'test-3' }) + it('should respond correctly to /plugin-d', async function () { + const res = await app.inject({ url: '/plugin-d' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-3' }) }) - app.inject({ - url: '/plugin-e' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'test-4-root' }) + it('should respond correctly to /plugin-e', async function () { + const res = await app.inject({ url: '/plugin-e' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'test-4-root' }) }) - app.inject({ - url: '/plugin-y' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { data: 'y' }) + it('should respond correctly to /plugin-y', async function () { + const res = await app.inject({ url: '/plugin-y' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { data: 'y' }) }) }) diff --git a/test/module/route-parameters.js b/test/module/route-parameters.js index d3df4c41..afeb82d6 100644 --- a/test/module/route-parameters.js +++ b/test/module/route-parameters.js @@ -1,81 +1,124 @@ -import t from 'tap' +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import routeParametersBasic from './route-parameters/basic.mjs' import routeParametersDisabled from './route-parameters/disabled.mjs' -const { test } = t - -test('routeParams: Default behaviour', async () => { +describe('routeParams: Default behaviour', function () { const app = fastify() - let res - - app.register(routeParametersBasic) - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/' }) - - res = await app.inject('/pages') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages' }) - res = await app.inject('/pages/archived') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/archived' }) - - res = await app.inject('/pages/test_id') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/:id/', id: 'test_id' }) - - res = await app.inject('/pages/test_id/edit') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/:id/edit', id: 'test_id' }) - - res = await app.inject('/users/test_id/details') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/users/:id/details', id: 'test_id' }) + before(async function () { + app.register(routeParametersBasic) + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should handle root route correctly', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/' }) + }) + + it('should handle /pages route correctly', async function () { + const res = await app.inject('/pages') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages' }) + }) + + it('should handle /pages/archived route correctly', async function () { + const res = await app.inject('/pages/archived') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages/archived' }) + }) + + it('should handle /pages/:id route correctly', async function () { + const res = await app.inject('/pages/test_id') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages/:id/', id: 'test_id' }) + }) + + it('should handle /pages/:id/edit route correctly', async function () { + const res = await app.inject('/pages/test_id/edit') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + route: '/pages/:id/edit', + id: 'test_id', + }) + }) + + it('should handle /users/:id/details route correctly', async function () { + const res = await app.inject('/users/test_id/details') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { + route: '/users/:id/details', + id: 'test_id', + }) + }) }) -test('routeParams: off', async () => { +describe('routeParams: off', function () { const app = fastify() - let res - - app.register(routeParametersDisabled) - - await app.ready() - - res = await app.inject('/') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/' }) - - res = await app.inject('/pages') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages' }) - - res = await app.inject('/pages/archived') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/archived' }) - - res = await app.inject('/pages/test_id') - t.equal(res.statusCode, 404) - - res = await app.inject('/pages/test_id/edit') - t.equal(res.statusCode, 404) - - res = await app.inject('/pages/_id') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/:id/' }) - - res = await app.inject('/pages/_id/edit') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/pages/:id/edit' }) - - res = await app.inject('/users/test_id/details') - t.equal(res.statusCode, 404) - res = await app.inject('/users/_id/details') - t.equal(res.statusCode, 200) - t.same(res.json(), { route: '/users/:id/details' }) + before(async function () { + app.register(routeParametersDisabled) + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should handle root route correctly', async function () { + const res = await app.inject('/') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/' }) + }) + + it('should handle /pages route correctly', async function () { + const res = await app.inject('/pages') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages' }) + }) + + it('should handle /pages/archived route correctly', async function () { + const res = await app.inject('/pages/archived') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages/archived' }) + }) + + it('should handle /pages/test_id route correctly', async function () { + const res = await app.inject('/pages/test_id') + assert.strictEqual(res.statusCode, 404) + }) + + it('should handle /pages/test_id/edit route correctly', async function () { + const res = await app.inject('/pages/test_id/edit') + assert.strictEqual(res.statusCode, 404) + }) + + it('should handle /pages/_id route correctly', async function () { + const res = await app.inject('/pages/_id') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages/:id/' }) + }) + + it('should handle /pages/_id/edit route correctly', async function () { + const res = await app.inject('/pages/_id/edit') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/pages/:id/edit' }) + }) + + it('should handle /users/test_id/details route correctly', async function () { + const res = await app.inject('/users/test_id/details') + assert.strictEqual(res.statusCode, 404) + }) + + it('should handle /users/_id/details route correctly', async function () { + const res = await app.inject('/users/_id/details') + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { route: '/users/:id/details' }) + }) }) diff --git a/test/typescript-esm/forceESM.ts b/test/typescript-esm/forceESM.ts index be9bb771..c694cfb7 100644 --- a/test/typescript-esm/forceESM.ts +++ b/test/typescript-esm/forceESM.ts @@ -1,27 +1,27 @@ +import test, { describe, before, after } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' + import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' -import t from 'tap' import fastifyAutoLoad from '../../index.js' -t.plan(4) - const __dirname = dirname(fileURLToPath(import.meta.url)) -const app = fastify() -app.register(fastifyAutoLoad, { dir: resolve(__dirname, 'app'), forceESM: true }) +describe('typescript/basic test suite', function () { + const app = fastify() + before(async function () { + app.register(fastifyAutoLoad, { dir: resolve(__dirname, 'app'), forceESM: true }) + await app.ready() + }) -app.ready(function (err): void { - t.error(err) + after(async function () { + await app.close() + }) - app.inject( - { - url: '/installed' - }, - function (err, res: any): void { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { result: 'ok' }) - } - ) + test('should load routes and respond correctly', async function () { + const res = await app.inject({ url: '/installed' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { result: 'ok' }) + }) }) diff --git a/test/typescript/basic.ts b/test/typescript/basic.ts index ddc65bb3..c3baa7ee 100644 --- a/test/typescript/basic.ts +++ b/test/typescript/basic.ts @@ -1,32 +1,29 @@ -import t from 'tap' +import test, { describe, before, after } from 'node:test' +import assert from 'node:assert' import fastify from 'fastify' import basicApp from './basic/app' -t.plan(5) - -const app = fastify() - -app.register(basicApp) +describe('typescript/basic test suite', function () { + const app = fastify() + before(async function () { + app.register(basicApp) + await app.ready() + }) -app.ready(async function (err): Promise { - t.error(err) + after(async function () { + await app.close() + }) - await app.inject({ - url: '/javascript' - }).then(function (res): void { - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { script: 'java' }) - }).catch((err) => { - t.error(err) + test('should respond correctly to /javascript', async function () { + const res = await app.inject({ url: '/javascript' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { script: 'java' }) }) - await app.inject({ - url: '/typescript' - }).then(function (res): void { - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.payload), { script: 'type' }) - }).catch((err) => { - t.error(err) + test('should respond correctly to /typescript', async function () { + const res = await app.inject({ url: '/typescript' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { script: 'type' }) }) })