New:Socket for Asana Is Now Available.Learn more
Get Started

@fastify/autoload

Package Overview
Dependencies
Maintainers
19
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/autoload - npm Package Compare versions

Comparing version
6.0.3
to
6.1.0
+4
.borp.yaml
files:
- 'test/issues/*/test.js'
- 'test/commonjs/*.js'
- 'test/module/*.js'
'use strict'
const { exec } = require('node:child_process')
const runtime = require('../lib/runtime')
function common () {
const args = ['node', 'test/typescript-common/index.ts']
const child = exec(args.join(' '), {
shell: true,
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
child.once('close', esm)
}
function esm () {
const args = ['node', 'test/typescript-esm/forceESM.ts']
const child = exec(args.join(' '), {
shell: true,
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
child.once('close', process.exit)
}
if (runtime.nodeVersion >= 23) {
common()
}
import type { FastifyInstance } from 'fastify'
const { join } = require('node:path')
const fastifyAutoLoad = require('../../../index.js')
module.exports = function (fastify: FastifyInstance, _opts: any, next: any) {
fastify.register(fastifyAutoLoad, {
dir: join(__dirname, 'foo'),
})
next()
}
'use strict'
module.exports = function (fastify, _opts, next) {
fastify.get('/javascript', (_request, reply) => {
reply.send({ script: 'java' })
})
next()
}
// See https://github.com/fastify/fastify-autoload/issues/65
declare const definitionFilesAreIncluded: never
export default definitionFilesAreIncluded
module.exports = function (fastify: any, _opts, next) {
fastify.get('/typescript', (_request, reply) => {
reply.send({ script: 'type' })
})
next()
}
'use script'
const t = require('tap')
const fastify = require('fastify')
const basicApp = require('./basic/app.ts')
t.plan(5)
const app = fastify()
app.register(basicApp)
app.ready(async function (err) {
t.error(err)
await app
.inject({
url: '/javascript',
})
.then(function (res: any) {
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { script: 'java' })
})
.catch((err) => {
t.error(err)
})
await app
.inject({
url: '/typescript',
})
.then(function (res: any) {
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { script: 'type' })
})
.catch((err) => {
t.error(err)
})
})
+1
-1

@@ -12,3 +12,3 @@ version: 2

schedule:
interval: "weekly"
interval: "monthly"
open-pull-requests-limit: 10

@@ -147,5 +147,12 @@ 'use strict'

function handleTypeScriptSupport (file, language, isHook = false) {
if (language === 'typescript' && !runtime.supportTypeScript) {
throw new Error(`@fastify/autoload cannot import ${isHook ? 'hooks ' : ''}plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`)
if (
language === 'typescript' &&
/* c8 ignore start - This cannot be reached from Node 23 native type-stripping */
!runtime.supportTypeScript
) {
throw new Error(
`@fastify/autoload cannot import ${isHook ? 'hooks ' : ''}plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`
)
}
/* c8 ignore end */
}

@@ -165,3 +172,3 @@

const typescriptPattern = /\.(ts|mts|cts)$/iu
const typescriptPattern = /\.(?:ts|mts|cts)$/iu
function getScriptType (fname, packageType) {

@@ -174,4 +181,4 @@ return {

const modulePattern = /\.(mjs|mts)$/iu
const commonjsPattern = /\.(cjs|cts)$/iu
const modulePattern = /\.(?:mjs|mts)$/iu
const commonjsPattern = /\.(?:cjs|cts)$/iu
function determineModuleType (fname, defaultType) {

@@ -178,0 +185,0 @@ if (modulePattern.test(fname)) {

@@ -119,3 +119,4 @@ 'use strict'

runtime.esbuild ||
runtime.tsimp
runtime.tsimp ||
runtime.nodeVersion >= 23
)

@@ -134,3 +135,9 @@ return cache.supportTypeScript

}
}
},
nodeVersion: {
get () {
cache.nodeVersion ??= Number(process.version.split('.')[0].slice(1))
return cache.nodeVersion
},
},
})

@@ -137,0 +144,0 @@

{
"name": "@fastify/autoload",
"version": "6.0.3",
"version": "6.1.0",
"description": "Require all plugins in a directory",

@@ -11,3 +11,3 @@ "main": "index.js",

"lint:fix": "eslint --fix",
"test": "npm run typescript && npm run typescript:jest && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild && npm run unit",
"test": "npm run typescript && npm run typescript:native && npm run typescript:jest && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild && npm run unit",
"typescript": "tsd",

@@ -21,6 +21,6 @@ "typescript:jest": "jest",

"typescript:esbuild": "node scripts/unit-typescript-esbuild.js",
"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"
},

@@ -80,2 +80,3 @@ "repository": {

"@fastify/url-data": "^6.0.0",
"@jsumners/line-reporter": "^1.0.1",
"@swc-node/register": "^1.9.1",

@@ -85,10 +86,10 @@ "@swc/core": "^1.5.25",

"@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",
"fastify": "^5.0.0",
"fastify-plugin": "^5.0.0",
"jest": "^29.7.0",
"neostandard": "^0.11.9",
"tap": "^19.0.2",
"neostandard": "^0.12.0",
"ts-jest": "^29.1.4",

@@ -95,0 +96,0 @@ "ts-node": "^10.9.2",

+15
-14

@@ -25,3 +25,3 @@ # @fastify/autoload

Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin
Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
in the table above.

@@ -51,4 +51,4 @@ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.

import autoLoad from '@fastify/autoload'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import fastify from 'fastify'

@@ -93,7 +93,7 @@

Autoload can be customised using the following options:
Autoload can be customized using the following options:
- `dir` (required) - Base directory containing plugins to be loaded
Each script file within a directory is treated as a plugin unless the directory contains an index file (e.g. `index.js`). In that case only the index file (and the potential sub-directories) will be loaded.
Each script file within a directory is treated as a plugin unless the directory contains an index file (e.g. `index.js`). In which case, only the index file (and the potential sub-directories) will be loaded.

@@ -129,3 +129,3 @@ The following script types are supported:

- `matchFilter` (optional) - Filter matching any path that should be loaded. Can be a RegExp, a string or a function returning a boolean.
- `matchFilter` (optional) - Filter matching any path that should be loaded. Can be a RegExp, a string, or a function returning a boolean.

@@ -140,3 +140,3 @@ ```js

- `ignoreFilter` (optional) - Filter matching any path that should not be loaded. Can be a RegExp, a string or a function returning a boolean.
- `ignoreFilter` (optional) - Filter matching any path that should not be loaded. Can be a RegExp, a string ,or a function returning a boolean.

@@ -163,3 +163,3 @@ ```js

with a [customization hooks](https://nodejs.org/docs/latest/api/module.html#customization-hooks)
provider, such as `ts-node`. Otherwise, widening the acceptance extension here will result in error.
provider, such as `ts-node`. Otherwise, widening the acceptance extension here will result in an error.

@@ -269,3 +269,3 @@

Default behaviour of `autoHooks` is to apply hooks only to the level on which the `autohooks.js` file is found. Setting `cascadeHooks: true` will continue applying the hooks to any children.
Default behavior of `autoHooks` is to apply hooks only to the level on which the `autohooks.js` file is found. Setting `cascadeHooks: true` will continue applying the hooks to any children.

@@ -282,3 +282,3 @@ ```js

Default behaviour of `cascadeHooks` is to accumulate hooks as new `autohooks.js` files are discovered and cascade to children. Setting `overwriteHooks: true` will start a new hook cascade when new `autohooks.js` files are encountered.
Default behavior of `cascadeHooks` is to accumulate hooks as new `autohooks.js` files are discovered and cascade to children. Setting `overwriteHooks: true` will start a new hook cascade when new `autohooks.js` files are encountered.

@@ -330,2 +330,3 @@ ```js

## Override TypeScript detection using an environment variable
This plugin uses [native type stripping](https://nodejs.org/docs/latest-v23.x/api/typescript.html#modules-typescript) with Node 23 and later.

@@ -508,7 +509,7 @@ It is possible to override the automatic detection of a TypeScript-capable runtime using the `FASTIFY_AUTOLOAD_TYPESCRIPT` environment variable. If set to a truthy value Autoload will load `.ts` files, expecting that node has a TypeScript-capable loader.

The autohooks functionality provides several options for automatically embedding hooks, decorators, etc. to your routes. CJS and ESM `autohook` formats are supported.
The autohooks functionality provides several options for automatically adding hooks, decorators, etc. to your routes. CJS and ESM `autohook` formats are supported.
The default behaviour of `autoHooks: true` is to encapsulate the `autohooks.js` plugin with the contents of the folder containing the file. The `cascadeHooks: true` option encapsulates the hooks with the current folder contents and all subsequent children, with any additional `autohooks.js` files being applied cumulatively. The `overwriteHooks: true` option will re-start the cascade any time an `autohooks.js` file is encountered.
The default behavior of `autoHooks: true` is to encapsulate the `autohooks.js` plugin with the contents of the folder containing the file. The `cascadeHooks: true` option encapsulates the hooks with the current folder contents and all subsequent children, with any additional `autohooks.js` files being applied cumulatively. The `overwriteHooks: true` option will restart the cascade any time an `autohooks.js` file is encountered.
Plugins and hooks are encapsulated together by folder and registered on the `fastify` instance which loaded the `@fastify/autoload` plugin. For more information on how encapsulation works in Fastify, see: https://fastify.dev/docs/latest/Reference/Encapsulation/#encapsulation
Plugins and hooks are encapsulated together by folder and registered on the `fastify` instance that loaded the `@fastify/autoload` plugin. For more information on how encapsulation works in Fastify, see: https://fastify.dev/docs/latest/Reference/Encapsulation/#encapsulation

@@ -555,3 +556,3 @@ ### Example:

$ curl http://localhost:3000/standard-plugin/
{} # no hooks in this folder, so behaviour is unchanged
{} # no hooks in this folder, so behavior is unchanged

@@ -558,0 +559,0 @@ $ curl http://localhost:3000/hooked-plugin/

@@ -6,7 +6,6 @@ 'use strict'

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'

@@ -13,0 +12,0 @@ ]

@@ -8,2 +8,3 @@ 'use strict'

'--require=tsm',
'--test',
'test/typescript/basic.ts'

@@ -10,0 +11,0 @@ ]

'use strict'
const { exec } = require('node:child_process')
const runtime = require('../lib/runtime')
const version = Number(process.version.split('.')[0].slice(1))
const args = [
'npx',
version >= 18 ? '--node-options=--import=tsx' : '',
runtime.nodeVersion >= 18 ? '--node-options=--import=tsx' : '',
'tsnd',

@@ -11,0 +10,0 @@ 'test/typescript/basic.ts'

'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 autohooks-basic', function () {
const app = Fastify()
before(async function () {
app.register(require('./autohooks/basic'))
app.decorateRequest('hooked', '')
await app.ready()
})
const app = Fastify()
after(async function () {
await app.close()
})
app.register(require('./autohooks/basic'))
app.decorateRequest('hooked', '')
it('should respond correctly to /', async function () {
const res = await app.inject({ url: '/' })
app.ready(function (err) {
t.error(err)
app.inject({
url: '/'
}, function (err, res) {
t.error(err)
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: '' })
})
})
'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 autohooks-cascade', function () {
const app = Fastify()
before(async function () {
app.register(require('./autohooks/cascade'))
app.decorateRequest('hooked', '')
await app.ready()
})
const app = Fastify()
after(async function () {
await app.close()
})
app.register(require('./autohooks/cascade'))
app.decorateRequest('hooked', '')
it('should respond correctly to /', async function () {
const res = await app.inject({ url: '/' })
app.ready(function (err) {
t.error(err)
app.inject({
url: '/'
}, function (err, res) {
t.error(err)
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'] })
})
})
'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 autohooks-disabled', function () {
const app = Fastify()
before(async function () {
app.register(require('./autohooks/disabled'))
app.decorateRequest('hooked', 'disabled')
await app.ready()
})
const app = Fastify()
after(async function () {
await app.close()
})
app.register(require('./autohooks/disabled'))
app.decorateRequest('hooked', 'disabled')
it('should respond correctly to /', async function () {
const res = await app.inject({ url: '/' })
app.ready(function (err) {
t.error(err)
app.inject({
url: '/'
}, function (err, res) {
t.error(err)
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' })
})
})
'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 autohooks-overwrite', function () {
const app = Fastify()
before(async function () {
app.register(require('./autohooks/overwrite'))
app.decorateRequest('hooked', '')
await app.ready()
})
const app = Fastify()
after(async function () {
await app.close()
})
app.register(require('./autohooks/overwrite'))
app.decorateRequest('hooked', '')
it('should respond correctly to /', async function () {
const res = await app.inject({ url: '/' })
app.ready(function (err) {
t.error(err)
app.inject({
url: '/'
}, function (err, res) {
t.error(err)
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'] })
})
})
'use strict'
const t = require('tap')
const { after, before, describe, it } = require('node:test')
const assert = require('node:assert')
const Fastify = require('fastify')

@@ -8,28 +9,33 @@ const AutoLoad = require('../../')

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' })
})
})
'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()
before(async function () {
app.register(require('./basic/app'))
await app.ready()
})
app.register(require('./basic/app'))
after(async function () {
await app.close()
})
app.ready(function (err) {
t.error(err)
it('should set app.foo correctly', function () {
assert.strictEqual(app.foo, 'bar')
})
t.equal(app.foo, 'bar')
it('should respond correctly to /something', async function () {
const res = await app.inject({ url: '/something' })
app.inject({
url: '/something'
}, function (err, res) {
t.error(err)
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 })
})
})
'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' })
})
})
'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()
before(async function () {
app.register(require('./deep/app'))
await app.ready()
})
app.register(require('./deep/app'))
after(async function () {
await app.close()
})
app.ready(function (err) {
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'
})
app.inject({
url: '/with-dirs/level-1/level-2/deep-route'
}, function (err, res) {
t.error(err)
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' })
})
})
'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')
})
})
'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.test('independent of module support', function (t) {
t.plan(8)
const app = fastify()
const runtime = require('../../lib/runtime')
app.register(require('./syntax-error/app'))
const typeStrippingEnabled = runtime.nodeVersion >= 23
app.ready(function (err) {
t.type(err, SyntaxError)
t.match(err.message, /unexpected token/i)
describe('independent module support - unexpected token', function () {
const app = Fastify()
before(async function () {
app.register(require('./syntax-error/app'))
})
const app2 = fastify()
after(async function () {
await app.close()
})
app2.register(require('./index-error/app'))
it('should return unexpected token error', async function () {
await assert.rejects(app.ready(), SyntaxError, /unexpected token/i)
})
})
app2.ready(function (err) {
t.type(err, Error)
t.match(err.message, /cannot import plugin.*index/i)
describe('independent module support - cannot import plugin index', function () {
const app = Fastify()
before(async function () {
app.register(require('./index-error/app'))
})
const app3 = fastify()
after(async function () {
await app.close()
})
app3.register(require('./ts-error/app'))
it('should return cannot import plugin index error', async function () {
await assert.rejects(app.ready(), Error, /cannot import plugin.*index/i)
})
})
app3.ready(function (err) {
t.type(err, Error)
t.match(err.message, /cannot import plugin.*typescript/i)
describe('independent module support - cannot import plugin typescript', function () {
const app = Fastify()
before(async function () {
app.register(require('./ts-error/app'))
})
const app4 = fastify()
after(async function () {
await app.close()
})
app4.register(require('./cyclic-dependency/app'))
it('should return cannot import plugin typescript error', async function () {
if (typeStrippingEnabled) {
assert.doesNotThrow(() => app.ready())
} else {
await assert.rejects(app.ready(), Error, /cannot import plugin.*typescript/i)
}
})
})
app4.ready(function (err) {
t.type(err, Error)
t.equal(err.message, 'Cyclic dependency')
describe('independent module support - cyclic dependency error', function () {
const app = Fastify()
before(async function () {
app.register(require('./cyclic-dependency/app'))
})
after(async function () {
await app.close()
})
it('should return cannot import plugin typescript error', async function () {
await assert.rejects(app.ready(), new Error('Cyclic dependency'))
})
})
'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)
})
})
'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 })
})
})

@@ -6,22 +6,24 @@ '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 non-plugin', function () {
const app = Fastify()
const app = fastify()
before(async function () {
app.register(require('./non-plugin/app'))
await app.ready()
})
app.register(require('./non-plugin/app'))
after(async function () {
await app.close()
})
app.ready(err => {
t.error(err)
it('should respond correctly to /foo', async function () {
const res = await app.inject({ url: '/foo' })
app.inject({
url: '/foo'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.payload, 'foo')
assert.strictEqual(res.statusCode, 200)
assert.strictEqual(res.payload, 'foo')
})
})
'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()
before(async function () {
app.decorate('root', 'root')
app.register(require('./options/app'))
await app.ready()
})
app.decorate('root', 'root')
after(async function () {
await app.close()
})
app.register(require('./options/app'))
it('should respond correctly to /plugin-a with global option override', async function () {
const res = await app.inject({ url: '/plugin-a' })
app.ready(function (err) {
t.error(err)
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' })
})
})
'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()
before(async function () {
app.register(require('./route-parameters/basic'))
await app.ready()
})
app.register(require('./route-parameters/basic'))
after(async function () {
await app.close()
})
app.ready(function (err) {
t.error(err)
it('should respond correctly to /users', async function () {
const res = await app.inject({ url: '/users' })
app.inject({
url: '/users'
}, function (err, res) {
t.error(err)
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',
})
})
})
'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()
before(async function () {
app.register(require('./route-parameters/disabled'))
await app.ready()
})
app.register(require('./route-parameters/disabled'))
after(async function () {
await app.close()
})
app.ready(function (err) {
t.error(err)
it('should respond correctly to /users', async function () {
const res = await app.inject({ url: '/users' })
app.inject({
url: '/users'
}, function (err, res) {
t.error(err)
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' } })
})
})
'use strict'
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
module.exports = async function (app) {
app.addHook('onRequest', async (req) => {
req.hooked = req.hooked || []

@@ -6,0 +6,0 @@ req.hooked.push('root')

'use strict'
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
module.exports = async function (app) {
app.addHook('onRequest', async (req) => {
req.hooked = req.hooked || []

@@ -6,0 +6,0 @@ req.hooked.push('child')

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ hooked: req.hooked })

'use strict'
module.exports = new Promise((resolve) => {
resolve(async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
resolve(async function (app) {
app.addHook('onRequest', async (req) => {
req.hooked = req.hooked || []

@@ -7,0 +7,0 @@ req.hooked.push('promisified')

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ hooked: req.hooked })

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ hooked: req.hooked })

'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 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
await assert.rejects(app.ready(), /Invalid or unexpected token/)
})
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')
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.`
)
)
}
})
await app.ready()
it("Should not accumulate plugin if doesn't comply to matchFilter", async function () {
const app = Fastify()
app.register(autoload, {
dir: path.join(__dirname, 'routes'),
})
const res = await app.inject({
url: '/'
})
await app.ready()
const res = await app.inject({ url: '/' })
assert.strictEqual(res.statusCode, 200)
t.equal(res.statusCode, 200)
const app2 = Fastify()
app2.register(autoload, {
dir: path.join(__dirname, 'routes'),
matchFilter: /invalid/,
})
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)
})
await app2.ready()
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',
})
const res2 = await app2.inject({
url: '/'
})
await app.ready()
const res = await app.inject({ url: '/' })
assert.strictEqual(res.statusCode, 200)
t.equal(res2.statusCode, 404)
})
const app2 = Fastify()
app2.register(autoload, {
dir: path.join(__dirname, 'routes'),
matchFilter: 'invalid-path',
})
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 app2.ready()
const res2 = await app2.inject({ url: '/' })
assert.strictEqual(res2.statusCode, 404)
})
await app.ready()
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'),
})
const res = await app.inject({
url: '/'
})
await app.ready()
const res = await app.inject({ url: '/' })
assert.strictEqual(res.statusCode, 200)
t.equal(res.statusCode, 200)
const app2 = Fastify()
app2.register(autoload, {
dir: path.join(__dirname, 'routes'),
matchFilter: (path) => path.includes('invalid-path'),
})
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)
})
await app2.ready()
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/,
})
const res2 = await app2.inject({
url: '/'
})
await app.ready()
const res = await app.inject({ url: '/' })
assert.strictEqual(res.statusCode, 200)
t.equal(res2.statusCode, 404)
})
const app2 = Fastify()
app2.register(autoload, {
dir: path.join(__dirname, 'routes'),
ignoreFilter: /\/routes.js/,
autoHooks: true,
})
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 app2.ready()
const res2 = await app2.inject({ url: '/' })
assert.strictEqual(res2.statusCode, 404)
})
await app.ready()
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,
})
const res = await app.inject({
url: '/'
})
app.decorateRequest('hooked', '')
await app.ready()
t.equal(res.statusCode, 200)
const res = await app.inject({ url: '/child' })
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(JSON.parse(res.payload), {
hooked: ['root', 'child'],
})
const app2 = Fastify()
app2.register(autoload, {
dir: path.join(__dirname, 'routes'),
matchFilter: (path) => path.includes('invalid-path')
const res2 = await app.inject({ url: '/promisified' })
assert.strictEqual(res2.statusCode, 200)
assert.deepStrictEqual(JSON.parse(res2.payload), { hooked: ['root'] })
})
await app2.ready()
it('Should not enrich non-SyntaxError', async function () {
const app = Fastify()
app.register(autoload, {
dir: path.join(__dirname, 'non-SyntaxError'),
autoHooks: true,
})
const res2 = await app2.inject({
url: '/'
await assert.rejects(app.ready(), new ReferenceError('x is not defined'))
})
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'))
})
'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ path: req.url, hooked: req.hooked, params: req.params })

'use strict'
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
module.exports = async function (app) {
app.addHook('onRequest', async (req) => {
req.hooked = req.hooked || []

@@ -6,0 +6,0 @@ req.hooked.push('root')

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ path: req.url, hooked: req.hooked })

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ path: req.url })

'use strict'
const t = require('tap')
const { after, before, describe, it } = require('node:test')
const assert = require('node:assert')
const path = require('node:path')

@@ -8,39 +9,43 @@ const Fastify = require('fastify')

t.plan(10)
describe('Issue 374 tests', function () {
const app = Fastify()
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' },
})
})
})
'use strict'
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
module.exports = async function (app) {
app.addHook('onRequest', async (req) => {
req.hooked = req.hooked || []

@@ -6,0 +6,0 @@ req.hooked.push('root')

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ path: req.url, hooked: req.hooked })

'use strict'
module.exports = async function (app, opts) {
module.exports = async function (app) {
app.get('/', async function (req, reply) {

@@ -5,0 +5,0 @@ reply.status(200).send({ path: req.url })

'use strict'
const t = require('tap')
const { after, before, describe, it } = require('node:test')
const assert = require('node:assert')
const path = require('node:path')

@@ -8,52 +9,57 @@ const Fastify = require('fastify')

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'],
})
})
})
'use strict'
module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
module.exports = async function (app) {
app.get('/', async function (_req, reply) {
reply.status(200).send({ tsx: 'ok' })
})
}
'use strict'
const t = require('tap')
const { afterEach, beforeEach, describe, it } = require('node:test')
const assert = require('node:assert')
const path = require('node:path')

@@ -8,38 +9,38 @@ const Fastify = require('fastify')

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: '/' })
assert.strictEqual(res.statusCode, 404)
})
})
const app2 = Fastify()
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.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
scriptPattern: /(js|ts|tsx)$/
})
const res = await app.inject({ url: '/' })
app2.ready(function (err) {
t.error(err)
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' })
})
})

@@ -1,2 +0,3 @@

import t from 'tap'
import { after, before, describe, it } from 'node:test'
import assert from 'node:assert'
import fastify from 'fastify'

@@ -8,106 +9,154 @@ import autoHooksBasic from './autohooks/basic.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', '')
before(async function () {
app.register(autoHooksBasic)
app.decorateRequest('hooked', '')
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root'] })
it('should respond correctly to /', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { hooked: ['root'] })
})
res = await app.inject('/child')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['child'] })
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'] })
})
res = await app.inject('/child/grandchild')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['grandchild'] })
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'] })
})
res = await app.inject('/sibling')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: '' })
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', '')
before(async function () {
app.register(autoHooksCascade)
app.decorateRequest('hooked', '')
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root'] })
it('should respond correctly to /', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { hooked: ['root'] })
})
res = await app.inject('/child')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root', 'child'] })
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'] })
})
res = await app.inject('/child/grandchild')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root', 'child', 'grandchild'] })
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'],
})
})
res = await app.inject('/sibling')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root'] })
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', '')
before(async function () {
app.register(autoHooksOverwrite)
app.decorateRequest('hooked', '')
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root'] })
it('should respond correctly to /', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { hooked: ['root'] })
})
res = await app.inject('/child')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['child'] })
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'] })
})
res = await app.inject('/child/grandchild')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['grandchild'] })
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'] })
})
res = await app.inject('/sibling')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: ['root'] })
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')
before(async function () {
app.register(autoHooksDisabled)
app.decorateRequest('hooked', 'disabled')
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { hooked: 'disabled' })
it('should respond correctly to /', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { hooked: 'disabled' })
})
res = await app.inject('/child')
t.equal(res.statusCode, 200)
t.same(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' })
})
res = await app.inject('/child/grandchild')
t.equal(res.statusCode, 200)
t.same(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' })
})
res = await app.inject('/sibling')
t.equal(res.statusCode, 200)
t.same(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' })
})
})

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,248 +0,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()
before(async function () {
app.register(basicApp)
await app.ready()
})
app.register(basicApp)
after(async function () {
await app.close()
})
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' })
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: '/autoroute/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 /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: '/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 /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: '/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 /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: '/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 /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: '/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 /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 })
})
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 /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: '/bar'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { foo: 'bar' })
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' })
})
app.inject({
url: '/prefixed'
}, function (err, res) {
t.error(err)
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,
})
})
})

@@ -1,5 +0,5 @@

import fs from 'fs'
import path, { dirname } from 'path'
import fs from 'node:fs'
import path, { dirname } from 'node:path'
import autoLoad from '../../../index.js'
import { fileURLToPath } from 'url'
import { fileURLToPath } from 'node:url'

@@ -6,0 +6,0 @@ const __filename = fileURLToPath(import.meta.url)

@@ -1,69 +0,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()
before(async function () {
app.register(dependencyApp)
await app.ready()
})
app.register(dependencyApp)
after(async function () {
await app.close()
})
app.ready(function (err) {
t.error(err)
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')
})
})

@@ -1,5 +0,5 @@

import path, { dirname } from 'path'
import path, { dirname } from 'node:path'
import fastifyUrlData from '@fastify/url-data'
import autoLoad from '../../../index.js'
import { fileURLToPath } from 'url'
import { fileURLToPath } from 'node:url'

@@ -6,0 +6,0 @@ const __filename = fileURLToPath(import.meta.url)

@@ -1,2 +0,3 @@

import t from 'tap'
import { afterEach, describe, it } from 'node:test'
import assert from 'node:assert'
import fastify from 'fastify'

@@ -8,53 +9,50 @@ import esmImportAppDefault from './esm-import/app-default.js'

const { test } = t
describe('ESM import tests', async function () {
let app
afterEach(async () => {
await app.close()
})
test('default', async () => {
const app = fastify()
it('default', async function () {
app = fastify()
app.register(esmImportAppDefault)
await app.ready()
app.register(esmImportAppDefault)
const res = await app.inject('/default')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { script: 'default' })
await app.close()
})
await app.ready()
it('named', async function () {
app = fastify()
app.register(esmImportAppNamed)
await app.ready()
const res = await app.inject('/default')
t.equal(res.statusCode, 200)
t.same(res.json(), { script: 'default' })
})
const res = await app.inject('/named')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { script: 'named' })
await app.close()
})
test('named', async () => {
const app = fastify()
it('star default', async function () {
app = fastify()
app.register(esmImportAppStarDefault)
await app.ready()
app.register(esmImportAppNamed)
const res = await app.inject('/star-default')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { script: 'star-default' })
await app.close()
})
await app.ready()
it('star named', async function () {
app = fastify()
app.register(esmImportAppStarNamed)
await app.ready()
const res = await app.inject('/named')
t.equal(res.statusCode, 200)
t.same(res.json(), { script: 'named' })
const res = await app.inject('/star-named')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { script: 'star-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' })
})

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import fastifyAutoloadDefault from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import { fastifyAutoload as fastifyAutoloadNamed } from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import * as fastifyAutoloadStar from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import * as fastifyAutoloadStar from '../../../index.js'

@@ -1,14 +0,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 })
})
})

@@ -1,3 +0,3 @@

import url from 'url'
import path from 'path'
import url from 'node:url'
import path from 'node:path'
import fastifyAutoload from '../../../index.js'

@@ -4,0 +4,0 @@

@@ -1,3 +0,3 @@

import url from 'url'
import process from 'process'
import url from 'node:url'
import process from 'node:process'

@@ -4,0 +4,0 @@ if (url.fileURLToPath(import.meta.url) === process.argv[1]) {

@@ -1,72 +0,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()
before(async function () {
app.decorate('root', 'root')
app.register(optionsApp)
await app.ready()
})
app.decorate('root', 'root')
after(async function () {
await app.close()
})
app.register(optionsApp)
app.ready(function (err) {
t.error(err)
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' })
})
})

@@ -1,4 +0,4 @@

import path from 'path'
import path from 'node:path'
import fastifyUrlData from '@fastify/url-data'
import { fileURLToPath } from 'url'
import { fileURLToPath } from 'node:url'
import autoLoad from '../../../index.js'

@@ -5,0 +5,0 @@

@@ -1,2 +0,3 @@

import t from 'tap'
import { after, before, describe, it } from 'node:test'
import assert from 'node:assert'
import fastify from 'fastify'

@@ -6,77 +7,119 @@ import routeParametersBasic from './route-parameters/basic.mjs'

const { test } = t
test('routeParams: Default behaviour', async () => {
describe('routeParams: Default behaviour', function () {
const app = fastify()
let res
app.register(routeParametersBasic)
before(async function () {
app.register(routeParametersBasic)
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/' })
it('should handle root route correctly', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { route: '/' })
})
res = await app.inject('/pages')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages' })
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' })
})
res = await app.inject('/pages/archived')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/archived' })
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' })
})
res = await app.inject('/pages/test_id')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/:id/', id: 'test_id' })
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' })
})
res = await app.inject('/pages/test_id/edit')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/:id/edit', 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',
})
})
res = await app.inject('/users/test_id/details')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/users/:id/details', 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)
before(async function () {
app.register(routeParametersDisabled)
await app.ready()
})
await app.ready()
after(async function () {
await app.close()
})
res = await app.inject('/')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/' })
it('should handle root route correctly', async function () {
const res = await app.inject('/')
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), { route: '/' })
})
res = await app.inject('/pages')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages' })
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' })
})
res = await app.inject('/pages/archived')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/archived' })
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' })
})
res = await app.inject('/pages/test_id')
t.equal(res.statusCode, 404)
it('should handle /pages/test_id route correctly', async function () {
const res = await app.inject('/pages/test_id')
assert.strictEqual(res.statusCode, 404)
})
res = await app.inject('/pages/test_id/edit')
t.equal(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)
})
res = await app.inject('/pages/_id')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/:id/' })
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/' })
})
res = await app.inject('/pages/_id/edit')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/pages/:id/edit' })
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' })
})
res = await app.inject('/users/test_id/details')
t.equal(res.statusCode, 404)
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)
})
res = await app.inject('/users/_id/details')
t.equal(res.statusCode, 200)
t.same(res.json(), { route: '/users/:id/details' })
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' })
})
})

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,3 +0,3 @@

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

@@ -4,0 +4,0 @@ import autoload from '../../../index.js'

@@ -1,2 +0,2 @@

import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

@@ -3,0 +3,0 @@ export default function (fastify: FastifyInstance, _: object, next: (err?: Error) => void): void {

@@ -0,27 +1,27 @@

import test, { describe, before, after } from 'node:test'
import assert from 'node:assert'
import fastify from 'fastify'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import t from 'tap'
import fastifyAutoLoad from '../../'
t.plan(4)
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import fastifyAutoLoad from '../../index.js'
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' })
})
})
import fastify from 'fastify'
import { join } from 'path'
import { join } from 'node:path'
import AutoLoad from '../../../'

@@ -4,0 +4,0 @@

import { FastifyPluginCallback } from 'fastify'
import { join } from 'path'
import { join } from 'node:path'
const fastifyAutoLoad = require('../../../index')
const app: FastifyPluginCallback = function (fastify, opts, next): void {
const app: FastifyPluginCallback = function (fastify, _opts, next): void {
fastify.register(fastifyAutoLoad, {

@@ -7,0 +7,0 @@ dir: join(__dirname, 'foo'),

import { FastifyPluginCallback } from 'fastify'
const plugin: FastifyPluginCallback = function (fastify, opts, next): void {
fastify.get('/typescript', (request, reply): void => {
const plugin: FastifyPluginCallback = function (fastify, _opts, next): void {
fastify.get('/typescript', (_request, reply): void => {
reply.send({ script: 'type' })

@@ -6,0 +6,0 @@ })

@@ -1,2 +0,2 @@

import { exec } from 'child_process'
import { exec } from 'node:child_process'

@@ -7,3 +7,3 @@ describe('integration test', function () {

async function (instance) {
await new Promise(function (resolve, reject) {
await new Promise(function (resolve) {
const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`)

@@ -10,0 +10,0 @@ let stderr = ''

@@ -1,2 +0,3 @@

import t from 'tap'
import test, { describe, before, after } from 'node:test'
import assert from 'node:assert'
import fastify from 'fastify'

@@ -6,28 +7,24 @@

t.plan(5)
describe('typescript/basic test suite', function () {
const app = fastify()
before(async function () {
app.register(basicApp)
await app.ready()
})
const app = fastify()
after(async function () {
await app.close()
})
app.register(basicApp)
app.ready(async function (err): Promise<void> {
t.error(err)
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' })
})
})
import { FastifyPluginCallback } from 'fastify'
import { join } from 'path'
import { join } from 'node:path'
import fastifyAutoLoad from '../../../'
const app: FastifyPluginCallback = function (fastify, opts, next): void {
const app: FastifyPluginCallback = function (fastify, _opts, next): void {
fastify.register(fastifyAutoLoad, {

@@ -7,0 +7,0 @@ dir: join(__dirname, 'foo'),

'use strict'
module.exports = function (fastify, opts, next) {
fastify.get('/javascript', (request, reply) => {
module.exports = function (fastify, _opts, next) {
fastify.get('/javascript', (_request, reply) => {
reply.send({ script: 'java' })

@@ -6,0 +6,0 @@ })

import { FastifyPluginCallback } from 'fastify'
const plugin: FastifyPluginCallback = function (fastify, opts, next): void {
fastify.get('/typescript', (request, reply): void => {
const plugin: FastifyPluginCallback = function (fastify, _opts, next): void {
fastify.get('/typescript', (_request, reply): void => {
reply.send({ script: 'type' })

@@ -6,0 +6,0 @@ })

@@ -6,3 +6,3 @@ 'use strict'

import AutoLoad from '../../index'
import { join } from 'path'
import { join } from 'node:path'

@@ -9,0 +9,0 @@ describe.concurrent('Vitest test suite', function () {

@@ -6,3 +6,3 @@ 'use strict'

import AutoLoad from '../../index'
import { join } from 'path'
import { join } from 'node:path'

@@ -9,0 +9,0 @@ describe.concurrent('Vitest ignore filters test suite', function () {

Sorry, the diff of this file is not supported yet

'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)