diff --git a/index.js b/index.js index ffaa2cbe..7392b264 100644 --- a/index.js +++ b/index.js @@ -159,8 +159,8 @@ function registerNode (node, fastify) { } function findCommonHooksPrefix (node) { - const prefixes = Object.values(node.pluginsMeta) - .map((meta) => meta?.options?.prefix) + const prefixes = node.plugins + .map((plugin) => node.pluginsMeta[plugin.file]?.options?.prefix) .filter((prefix) => typeof prefix === 'string' && prefix.length > 0) if (prefixes.length === 0) { diff --git a/test/commonjs/autohooks-basic-mini.js b/test/commonjs/autohooks-basic-mini.js new file mode 100644 index 00000000..2fff595f --- /dev/null +++ b/test/commonjs/autohooks-basic-mini.js @@ -0,0 +1,32 @@ +'use strict' + +const { after, before, describe, it } = require('node:test') +const assert = require('node:assert') +const Fastify = require('fastify') + +describe('Node test suite for autohooks-basic', function () { + const app = Fastify() + before(async function () { + app.register(require('./autohooks/basic-mini')) + app.decorateRequest('hooked', '') + await app.ready() + }) + + after(async function () { + await app.close() + }) + + it('should respond correctly to /', async function () { + const res = await app.inject({ url: '/' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] }) + }) + + it('should respond correctly to /child', async function () { + const res = await app.inject({ url: '/child' }) + + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(JSON.parse(res.payload), { hooked: '' }) + }) +}) diff --git a/test/commonjs/autohooks/basic-mini.js b/test/commonjs/autohooks/basic-mini.js new file mode 100644 index 00000000..fc4a8ac3 --- /dev/null +++ b/test/commonjs/autohooks/basic-mini.js @@ -0,0 +1,15 @@ +'use strict' + +const path = require('node:path') +const autoLoad = require('../../../') + +module.exports = function (fastify, opts, next) { + fastify.log.error(__dirname) + + fastify.register(autoLoad, { + dir: path.join(__dirname, 'routes-mini'), + autoHooks: true + }) + + next() +} diff --git a/test/commonjs/autohooks/routes-mini/.autohooks.js b/test/commonjs/autohooks/routes-mini/.autohooks.js new file mode 100644 index 00000000..1e551aef --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/.autohooks.js @@ -0,0 +1,6 @@ +module.exports = async function (app, opts) { + app.addHook('onRequest', async (req, reply) => { + req.hooked = req.hooked || [] + req.hooked.push('root') + }) +} diff --git a/test/commonjs/autohooks/routes-mini/child/routes.js b/test/commonjs/autohooks/routes-mini/child/routes.js new file mode 100644 index 00000000..521b6527 --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/child/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app, opts) { + app.get('/', async function (req, reply) { + reply.status(200).send({ hooked: req.hooked }) + }) +} diff --git a/test/commonjs/autohooks/routes-mini/routes.js b/test/commonjs/autohooks/routes-mini/routes.js new file mode 100644 index 00000000..521b6527 --- /dev/null +++ b/test/commonjs/autohooks/routes-mini/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app, opts) { + app.get('/', async function (req, reply) { + reply.status(200).send({ hooked: req.hooked }) + }) +}