From 865a454a9c426a519bf6a7bb0265380f60e6ecc6 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 11 Jul 2022 12:59:03 +0200 Subject: [PATCH] Make sure we always start in a microtick (#189) Signed-off-by: Matteo Collina Fixes: https://github.com/fastify/fastify/issues/4125 --- boot.js | 2 +- test/await-use.test.js | 22 ++++++++++++++++++++++ test/chainable.test.js | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/boot.js b/boot.js index f6f7c29..bac9fd7 100644 --- a/boot.js +++ b/boot.js @@ -334,7 +334,7 @@ Boot.prototype.ready = function (func) { throw new AVV_ERR_CALLBACK_NOT_FN('ready', typeof func) } this._readyQ.push(func) - this.start() + queueMicrotask(this.start.bind(this)) return } diff --git a/test/await-use.test.js b/test/await-use.test.js index b8c173d..6cd5db1 100644 --- a/test/await-use.test.js +++ b/test/await-use.test.js @@ -270,3 +270,25 @@ test('await use - mix of same and future tick callbacks', async (t) => { record += 'ready' t.equal(record, 'plugin0|plugin1|plugin2|ready') }) + +test('await use - fork the promise chain', { only: true }, (t) => { + t.plan(3) + const app = {} + boot(app, { autostart: false }) + + async function setup () { + let set = false + await app.use(async function plugin0 () { + t.pass('plugin0 init') + await sleep(500) + set = true + }) + t.equal(set, true) + } + setup() + + app.ready((err, done) => { + t.error(err) + done() + }) +}) diff --git a/test/chainable.test.js b/test/chainable.test.js index 67ecc6d..b889456 100644 --- a/test/chainable.test.js +++ b/test/chainable.test.js @@ -63,7 +63,7 @@ test('chainable automatically binded', (t) => { expose[key] = 'muahah' boot(app, { - expose: expose + expose }) t.end()