Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function preloadESModules (opts) {
if (typeof opts.import === 'string') {
opts.import = [opts.import]
}
opts.import.forEach(async (m) => {
for (const m of opts.import) {
if (m) {
/* This check ensures we ignore `-i ""`, trailing `-i`, or
* other silly things the user might (inadvertently) be doing.
Expand All @@ -96,7 +96,7 @@ async function preloadESModules (opts) {
module.exports.stop(e)
}
}
})
}
}

async function runFastify (args, additionalOptions, serverOptions, serverModule) {
Expand Down
30 changes: 30 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,36 @@ test('should support preloading custom ES module', async t => {
t.pass('server closed')
})

for (const modules of [['first.mjs'], ['first.mjs', 'second.mjs']]) {
test(`should finish ES module preloads before loading the app (${modules.length} modules)`, async t => {
const events = []
const pendingImports = []
const startWithAsyncImports = proxyquire('../start', {
'./util': {
requireESModule (name) {
events.push(`start:${name}`)
const imported = new Promise(resolve => setImmediate(resolve)).then(() => {
events.push(`end:${name}`)
})
pendingImports.push(imported)
return imported
},
async requireServerPluginFromPath () {
events.push('load app')
return async function () {}
}
}
})
const args = modules.flatMap(name => ['--import', name])
args.push('./examples/plugin.js')
const fastify = await startWithAsyncImports.runFastify(args, { ready: true })
t.teardown(() => fastify.close())
await Promise.all(pendingImports)

t.same(events, [...modules.flatMap(name => [`start:${name}`, `end:${name}`]), 'load app'])
})
}

test('should support preloading multiple custom modules', async t => {
t.plan(3)

Expand Down
Loading