From 0a63dd4b20588876d065a082333a14ffc24655c8 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 10 Jul 2024 10:38:46 +0200 Subject: [PATCH 1/6] fix: should ensure tests can access all decorators --- helper.d.ts | 6 +++++- helper.js | 10 ++++++++-- start.js | 15 +++++++++------ templates/app-ts/test/helper.ts | 9 +++++---- test/helper.test.js | 7 +++++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/helper.d.ts b/helper.d.ts index f8642543..c94976d2 100644 --- a/helper.d.ts +++ b/helper.d.ts @@ -1,8 +1,12 @@ import fastify from 'fastify' +type BuildOptions = { + skipOverride?: boolean +} + declare module 'fastify-cli/helper.js' { module helper { - export function build(args: Array, additionalOptions?: Object, serverOptions?: Object): ReturnType; + export function build(args: Array, additionalOptions?: Object, serverOptions?: Object, buildOptions?: BuildOptions): ReturnType; } export = helper; diff --git a/helper.js b/helper.js index bfb893b9..e654d501 100644 --- a/helper.js +++ b/helper.js @@ -3,13 +3,19 @@ const { runFastify } = require('./start') module.exports = { - build (args, additionalOptions = {}, serverOptions = {}) { + build (args, additionalOptions = {}, serverOptions = {}, buildOptions = {}) { Object.defineProperty(additionalOptions, 'ready', { value: true, enumerable: false, writable: false }) - return runFastify(args, additionalOptions, serverOptions) + + const buildOpts = { + skipOverride: true, + ...buildOptions + } + + return runFastify(args, additionalOptions, serverOptions, buildOpts) }, listen: runFastify } diff --git a/start.js b/start.js index 4c4cdf07..8e77e24d 100755 --- a/start.js +++ b/start.js @@ -21,6 +21,7 @@ const { showHelpForCommand, isKubernetes } = require('./util') +const fp = require('fastify-plugin') let Fastify = null @@ -94,7 +95,7 @@ async function preloadESModules (opts) { }) } -async function runFastify (args, additionalOptions, serverOptions) { +async function runFastify (args, additionalOptions = {}, serverOptions = {}, buildOptions = {}) { const opts = parseArgs(args) if (opts.require) { @@ -155,9 +156,7 @@ async function runFastify (args, additionalOptions, serverOptions) { } } - if (serverOptions) { - options = deepmerge(options, serverOptions) - } + options = deepmerge(options, serverOptions) if (opts.options && file.options) { options = deepmerge(options, file.options) @@ -170,7 +169,11 @@ 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 fn = file.default || file + const app = buildOptions.skipOverride ? fp(fn) : fn + + await fastify.register(app, appConfig) const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) { if (err) { @@ -184,7 +187,7 @@ async function runFastify (args, additionalOptions, serverOptions) { done() }) - if (additionalOptions && additionalOptions.ready) { + if (additionalOptions.ready) { await fastify.ready() } else if (opts.address) { await fastify.listen({ port: opts.port, host: opts.address }) diff --git a/templates/app-ts/test/helper.ts b/templates/app-ts/test/helper.ts index 70451775..f2d082d9 100644 --- a/templates/app-ts/test/helper.ts +++ b/templates/app-ts/test/helper.ts @@ -17,14 +17,15 @@ async function config () { // Automatically build and tear down our instance async function build (t: TestContext) { - // you can set all the options supported by the fastify CLI command + // You can set all the options supported by the fastify CLI command const argv = [AppPath] - // fastify-plugin ensures that all decorators - // are exposed for testing purposes, this is - // different from the production setup const app = await helper.build(argv, await config()) + // Ensures that all decorators are exposed for testing purposes, + // this is different from the production setup + app[Symbol.for("skip-override")] = true; + // Tear down our app after we are done t.after(() => void app.close()) diff --git a/test/helper.test.js b/test/helper.test.js index 20607d9f..2b044c5c 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -150,3 +150,10 @@ 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.listen(argv, {}) + t.teardown(() => app.close()) + t.ok(app.test) +}) From ca4084a875ff25bee8ca54d0eff327ce811c3bcb Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 10 Jul 2024 10:46:18 +0200 Subject: [PATCH 2/6] refactor: use more appropriate name --- start.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/start.js b/start.js index 8e77e24d..13dc3755 100755 --- a/start.js +++ b/start.js @@ -170,10 +170,9 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui const appConfig = Object.assign({}, opts.options ? file.options : {}, opts.pluginOptions, additionalOptions) - const fn = file.default || file - const app = buildOptions.skipOverride ? fp(fn) : fn - - await fastify.register(app, appConfig) + const appFn = file.default || file + const appPlugin = buildOptions.skipOverride ? fp(appFn) : appFn + await fastify.register(appPlugin, appConfig) const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) { if (err) { From d440e821280df5fe18e32b3d7798b81e12008756 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 10 Jul 2024 10:47:51 +0200 Subject: [PATCH 3/6] refactor: revert template changes --- templates/app-ts/test/helper.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/templates/app-ts/test/helper.ts b/templates/app-ts/test/helper.ts index f2d082d9..70451775 100644 --- a/templates/app-ts/test/helper.ts +++ b/templates/app-ts/test/helper.ts @@ -17,15 +17,14 @@ async function config () { // Automatically build and tear down our instance async function build (t: TestContext) { - // You can set all the options supported by the fastify CLI command + // you can set all the options supported by the fastify CLI command const argv = [AppPath] + // fastify-plugin ensures that all decorators + // are exposed for testing purposes, this is + // different from the production setup const app = await helper.build(argv, await config()) - // Ensures that all decorators are exposed for testing purposes, - // this is different from the production setup - app[Symbol.for("skip-override")] = true; - // Tear down our app after we are done t.after(() => void app.close()) From 7f7337573623a65e51764fe6e975f4d575089a42 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 10 Jul 2024 11:33:09 +0200 Subject: [PATCH 4/6] fix: revert add default value to runFastify params --- start.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/start.js b/start.js index 13dc3755..6c9fa4db 100755 --- a/start.js +++ b/start.js @@ -95,7 +95,7 @@ async function preloadESModules (opts) { }) } -async function runFastify (args, additionalOptions = {}, serverOptions = {}, buildOptions = {}) { +async function runFastify (args, additionalOptions, serverOptions, buildOptions = {}) { const opts = parseArgs(args) if (opts.require) { @@ -156,7 +156,9 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui } } - options = deepmerge(options, serverOptions) + if (serverOptions) { + options = deepmerge(options, serverOptions) + } if (opts.options && file.options) { options = deepmerge(options, file.options) @@ -186,7 +188,7 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui done() }) - if (additionalOptions.ready) { + if (additionalOptions && additionalOptions.ready) { await fastify.ready() } else if (opts.address) { await fastify.listen({ port: opts.port, host: opts.address }) From f1f97300ed827fc6730e9e8e4bd45ff8ea6aa4e9 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 10 Jul 2024 11:33:09 +0200 Subject: [PATCH 5/6] fix: revert add default value to runFastify params --- helper.js | 7 +------ start.js | 10 ++++++---- test/helper.test.js | 6 +++++- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/helper.js b/helper.js index e654d501..56541c46 100644 --- a/helper.js +++ b/helper.js @@ -10,12 +10,7 @@ module.exports = { writable: false }) - const buildOpts = { - skipOverride: true, - ...buildOptions - } - - return runFastify(args, additionalOptions, serverOptions, buildOpts) + return runFastify(args, additionalOptions, serverOptions, buildOptions) }, listen: runFastify } diff --git a/start.js b/start.js index 13dc3755..3366a6eb 100755 --- a/start.js +++ b/start.js @@ -95,7 +95,7 @@ async function preloadESModules (opts) { }) } -async function runFastify (args, additionalOptions = {}, serverOptions = {}, buildOptions = {}) { +async function runFastify (args, additionalOptions, serverOptions, buildOptions = {}) { const opts = parseArgs(args) if (opts.require) { @@ -156,7 +156,9 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui } } - options = deepmerge(options, serverOptions) + if (serverOptions) { + options = deepmerge(options, serverOptions) + } if (opts.options && file.options) { options = deepmerge(options, file.options) @@ -171,7 +173,7 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui const appConfig = Object.assign({}, opts.options ? file.options : {}, opts.pluginOptions, additionalOptions) const appFn = file.default || file - const appPlugin = buildOptions.skipOverride ? fp(appFn) : appFn + const appPlugin = appConfig.skipOverride ? fp(appFn) : appFn await fastify.register(appPlugin, appConfig) const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) { @@ -186,7 +188,7 @@ async function runFastify (args, additionalOptions = {}, serverOptions = {}, bui done() }) - if (additionalOptions.ready) { + if (additionalOptions && additionalOptions.ready) { await fastify.ready() } else if (opts.address) { await fastify.listen({ port: opts.port, host: opts.address }) diff --git a/test/helper.test.js b/test/helper.test.js index 2b044c5c..3d165b83 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -153,7 +153,11 @@ test('should merge the CLI and FILE configs', async t => { test('should ensure can access all decorators', async t => { const argv = ['./examples/plugin.js'] - const app = await helper.listen(argv, {}) + 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) }) From 884ae881cdd920a592142f7f490cbb4ab73d4390 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 24 Jul 2024 12:20:19 +0200 Subject: [PATCH 6/6] fix: remove buildOptions --- start.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.js b/start.js index 3366a6eb..e359369e 100755 --- a/start.js +++ b/start.js @@ -95,7 +95,7 @@ async function preloadESModules (opts) { }) } -async function runFastify (args, additionalOptions, serverOptions, buildOptions = {}) { +async function runFastify (args, additionalOptions, serverOptions) { const opts = parseArgs(args) if (opts.require) {