Skip to content

Commit 49e3272

Browse files
committed
fix: store unref promises for awaiting in tests
1 parent ab85bbe commit 49e3272

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

lib/npm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Npm {
2828
return require(`./commands/${command}.js`)
2929
}
3030

31+
unrefPromises = []
3132
updateNotification = null
3233
argv = []
3334

@@ -224,11 +225,11 @@ class Npm {
224225
log.verbose('argv', this.#argvClean.map(JSON.stringify).join(' '))
225226
})
226227

227-
this.#logFile.load({
228+
this.unrefPromises.push(this.#logFile.load({
228229
path: this.logPath,
229230
logsMax: this.config.get('logs-max'),
230231
timing: this.config.get('timing'),
231-
})
232+
}))
232233

233234
this.#timers.load({
234235
path: this.logPath,

test/fixtures/mock-npm.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ const getMockNpm = async (t, { mocks, init, load, npm: npmOpts }) => {
7373
})
7474
}
7575

76+
async load () {
77+
const res = await super.load()
78+
await Promise.all(this.unrefPromises)
79+
return res
80+
}
81+
7682
async exec (...args) {
7783
const [res, err] = await super.exec(...args).then((r) => [r]).catch(e => [null, e])
7884
// This mimics how the exit handler flushes output for commands that have

test/lib/cli/exit-handler.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ t.test('handles unknown error with logs and debug file', async (t) => {
129129
})
130130

131131
await exitHandler(err('Unknown error', 'ECODE'))
132-
// force logfile cleaning logs to happen since those are purposefully not awaited
133-
await require('timers/promises').setTimeout(200)
134132

135133
const fileLogs = await debugFile()
136134
const fileLines = fileLogs.split('\n')
@@ -140,19 +138,14 @@ t.test('handles unknown error with logs and debug file', async (t) => {
140138

141139
t.equal(process.exitCode, 1)
142140

143-
let skippedLogs = 0
144141
logs.forEach((logItem, i) => {
145142
const logLines = logItem.split('\n').map(l => `${i} ${l}`)
146143
for (const line of logLines) {
147-
if (line.includes('logfile') && line.includes('cleaning')) {
148-
skippedLogs++
149-
continue
150-
}
151144
t.match(fileLogs.trim(), line, 'log appears in debug file')
152145
}
153146
})
154147

155-
t.equal(logs.length - skippedLogs, parseInt(lastLog) + 1)
148+
t.equal(logs.length, parseInt(lastLog) + 1)
156149
t.match(logs.error, [
157150
'code ECODE',
158151
'ERR SUMMARY Unknown error',

0 commit comments

Comments
 (0)