From 9ad885512b29e2ec1d07291b0157613383bfabfc Mon Sep 17 00:00:00 2001 From: Dan Castillo Date: Fri, 28 Jul 2023 12:37:16 -0400 Subject: [PATCH] fix error on undefined plugin (#649) * fix error on undefined plugin * Apply suggestions from code review --- test/data/object.js | 1 + test/start.test.js | 12 ++++++++++++ util.js | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/data/object.js diff --git a/test/data/object.js b/test/data/object.js new file mode 100644 index 00000000..4ba52ba2 --- /dev/null +++ b/test/data/object.js @@ -0,0 +1 @@ +module.exports = {} diff --git a/test/start.test.js b/test/start.test.js index b37ef5f1..454e6732 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -1034,3 +1034,15 @@ test('should start fastify with custom plugin options with a CJS plugin with pac t.pass('server closed') t.end() }) + +test('should throw error for invalid fastify plugin (object)', async t => { + t.plan(1) + try { + const port = getPort() + const argv = ['-p', port, '-T', '100', './test/data/object.js'] + await start.start(argv) + t.fail('should not start') + } catch (err) { + t.equal(err.code, 'AVV_ERR_PLUGIN_NOT_VALID') + } +}) diff --git a/util.js b/util.js index fcf06de5..c86ebe36 100644 --- a/util.js +++ b/util.js @@ -42,7 +42,7 @@ function requireFastifyForModule (modulePath) { } function isInvalidAsyncPlugin (plugin) { - return plugin.length !== 2 && plugin.constructor.name === 'AsyncFunction' + return plugin && plugin.length !== 2 && plugin.constructor.name === 'AsyncFunction' } async function getPackageType (cwd) {