Skip to content

Commit

Permalink
fix: store unref promises for awaiting in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Apr 24, 2024
1 parent 6cc995c commit f56447d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Npm {
return require(`./commands/${command}.js`)
}

unrefPromises = []
updateNotification = null
argv = []

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions test/lib/cli/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand Down

0 comments on commit f56447d

Please sign in to comment.