diff --git a/lib/npm.js b/lib/npm.js index 9b00321556a44..fa60fe62ce19e 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -28,6 +28,7 @@ class Npm { return require(`./commands/${command}.js`) } + unrefPromises = [] updateNotification = null argv = [] @@ -224,11 +225,11 @@ class Npm { log.verbose('argv', this.#argvClean.map(JSON.stringify).join(' ')) }) - this.#logFile.load({ + this.unrefPromises.push(this.#logFile.load({ path: this.logPath, logsMax: this.config.get('logs-max'), timing: this.config.get('timing'), - }) + })) this.#timers.load({ path: this.logPath, diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 4a58b869858fd..4177698853138 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -73,6 +73,12 @@ const getMockNpm = async (t, { mocks, init, load, npm: npmOpts }) => { }) } + async load () { + const res = await super.load() + await Promise.all(this.unrefPromises) + return res + } + async exec (...args) { const [res, err] = await super.exec(...args).then((r) => [r]).catch(e => [null, e]) // This mimics how the exit handler flushes output for commands that have diff --git a/test/lib/cli/exit-handler.js b/test/lib/cli/exit-handler.js index 3cb4523b3ee51..fd6754b30096e 100644 --- a/test/lib/cli/exit-handler.js +++ b/test/lib/cli/exit-handler.js @@ -129,8 +129,6 @@ t.test('handles unknown error with logs and debug file', async (t) => { }) await exitHandler(err('Unknown error', 'ECODE')) - // force logfile cleaning logs to happen since those are purposefully not awaited - await require('timers/promises').setTimeout(200) const fileLogs = await debugFile() const fileLines = fileLogs.split('\n') @@ -140,19 +138,14 @@ t.test('handles unknown error with logs and debug file', async (t) => { t.equal(process.exitCode, 1) - let skippedLogs = 0 logs.forEach((logItem, i) => { const logLines = logItem.split('\n').map(l => `${i} ${l}`) for (const line of logLines) { - if (line.includes('logfile') && line.includes('cleaning')) { - skippedLogs++ - continue - } t.match(fileLogs.trim(), line, 'log appears in debug file') } }) - t.equal(logs.length - skippedLogs, parseInt(lastLog) + 1) + t.equal(logs.length, parseInt(lastLog) + 1) t.match(logs.error, [ 'code ECODE', 'ERR SUMMARY Unknown error',