From 9fa77f0cfd070fb3cff3544e4da5edd90570ca60 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 14 Aug 2023 13:50:30 +0200 Subject: [PATCH 1/2] fix: calling avvio with new (#230) --- boot.js | 12 +++++------- test/basic.test.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/boot.js b/boot.js index 5c1001c..af09afb 100644 --- a/boot.js +++ b/boot.js @@ -101,14 +101,12 @@ function Boot (server, opts, done) { opts = opts || {} - if (!(this instanceof Boot)) { - const instance = new Boot(server, opts, done) - - if (server) { - wrap(server, opts, instance) - } + if (!new.target) { + return new Boot(server, opts, done) + } - return instance + if (server) { + wrap(server, opts, this) } if (opts.autostart !== false) { diff --git a/test/basic.test.js b/test/basic.test.js index f7892f1..1f52f21 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -97,7 +97,7 @@ test('boot a plugin with a custom server', (t) => { }) }) -test('custom instance should inherits avvio methods', (t) => { +test('custom instance should inherits avvio methods /1', (t) => { t.plan(6) const server = {} @@ -126,6 +126,35 @@ test('custom instance should inherits avvio methods', (t) => { }) }) +test('custom instance should inherits avvio methods /2', (t) => { + t.plan(6) + + const server = {} + const app = new boot(server, {}) // eslint-disable-line new-cap + + server.use(function (s, opts, done) { + t.equal(s, server, 'the first argument is the server') + t.same(opts, {}, 'no options') + done() + }).after(() => { + t.ok('after called') + }) + + server.onClose(() => { + t.ok('onClose called') + }) + + server.ready(() => { + t.ok('ready called') + }) + + app.on('start', () => { + server.close(() => { + t.pass('booted') + }) + }) +}) + test('boot a plugin with options', (t) => { t.plan(3) From ed940d7121cba9650c8c6ce926c221eb6c08e6ed Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 14 Aug 2023 15:35:25 +0200 Subject: [PATCH 2/2] chore: jsdoc for debug (#228) --- lib/debug.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/debug.js b/lib/debug.js index 4519ad5..5eccad3 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -2,6 +2,18 @@ const { debuglog } = require('util') +/** + * @callback DebugLogger + * @param {string} msg + * @param {...unknown} param + * @returns {void} + */ + +/** + * @type {DebugLogger} + */ +const debug = debuglog('avvio') + module.exports = { - debug: debuglog('avvio') + debug }