From 6c83cd32d6475cb5bfa97fa77fdb86620d0c557a Mon Sep 17 00:00:00 2001 From: Jean <110341611+jean-michelet@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:34:20 +0200 Subject: [PATCH] fix: should ensure can access all decorators from app built with `helper.build` (#742) * fix: should ensure tests can access all decorators * refactor: use more appropriate name * refactor: revert template changes * fix: revert add default value to runFastify params * fix: revert add default value to runFastify params * fix: remove buildOptions --- README.md | 3 ++- helper.js | 1 + start.js | 6 +++++- test/helper.test.js | 11 +++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e61a40dd..5a51e74f 100644 --- a/README.md +++ b/README.md @@ -363,7 +363,8 @@ const assert = require('node:assert') test('test my application', async t => { const argv = ['app.js'] const app = await build(argv, { - extraParam: 'foo' + extraParam: 'foo', + skipOverride: true // If you want your application to be registered with fastify-plugin }) t.after(() => app.close()) diff --git a/helper.js b/helper.js index bfb893b9..609dc7f8 100644 --- a/helper.js +++ b/helper.js @@ -9,6 +9,7 @@ module.exports = { enumerable: false, writable: false }) + return runFastify(args, additionalOptions, serverOptions) }, listen: runFastify diff --git a/start.js b/start.js index 4c4cdf07..e359369e 100755 --- a/start.js +++ b/start.js @@ -21,6 +21,7 @@ const { showHelpForCommand, isKubernetes } = require('./util') +const fp = require('fastify-plugin') let Fastify = null @@ -170,7 +171,10 @@ async function runFastify (args, additionalOptions, serverOptions) { } const appConfig = Object.assign({}, opts.options ? file.options : {}, opts.pluginOptions, additionalOptions) - await fastify.register(file.default || file, appConfig) + + const appFn = file.default || file + const appPlugin = appConfig.skipOverride ? fp(appFn) : appFn + await fastify.register(appPlugin, appConfig) const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) { if (err) { diff --git a/test/helper.test.js b/test/helper.test.js index 20607d9f..3d165b83 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -150,3 +150,14 @@ test('should merge the CLI and FILE configs', async t => { t.same(lines.length, 1) t.same(lines[0].foo, '***') }) + +test('should ensure can access all decorators', async t => { + const argv = ['./examples/plugin.js'] + const app = await helper.build(argv, { skipOverride: true }) + t.teardown(() => app.close()) + t.ok(app.test) + + const app2 = await helper.listen(argv, { skipOverride: true }) + t.teardown(() => app2.close()) + t.ok(app2.test) +})