Skip to content

Commit bac99f4

Browse files
committed
Add test to check for async opts.log()
1 parent 45500cc commit bac99f4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/exit.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
const process = require('process')
44

5+
const promisify = require('util.promisify')
56
const test = require('ava')
67
const sinon = require('sinon')
78
const lolex = require('lolex')
89

10+
const pNextTick = promisify(process.nextTick)
11+
912
// Required directly inside `src` because this is exposed through
1013
// documentation, but not through code
1114
// eslint-disable-next-line import/no-internal-modules
@@ -79,5 +82,48 @@ repeatEvents((prefix, { eventName, emitEvent }) => {
7982

8083
unstubProcessExit({ clock, processExit })
8184
})
85+
86+
test(`${prefix} should delay process.exit(1) with async opts.log()`, async t => {
87+
const { clock, processExit } = stubProcessExit()
88+
89+
const { promise, resolve } = getPromise()
90+
91+
const { stopLogging } = startLogging({
92+
exitOn: [eventName],
93+
eventName,
94+
log: () => promise,
95+
})
96+
97+
await emitEventAndWait(EXIT_TIMEOUT, { clock, emitEvent })
98+
99+
t.true(processExit.notCalled)
100+
101+
await resolve()
102+
clock.tick(EXIT_TIMEOUT)
103+
104+
t.true(processExit.called)
105+
106+
stopLogging()
107+
108+
unstubProcessExit({ clock, processExit })
109+
})
82110
})
111+
112+
// Returns a promise that can be triggered from outside
113+
const getPromise = function() {
114+
// eslint-disable-next-line fp/no-let, init-declarations
115+
let resolveA
116+
// eslint-disable-next-line promise/avoid-new
117+
const promise = new Promise(resolve => {
118+
// eslint-disable-next-line fp/no-mutation
119+
resolveA = getResolve.bind(null, resolve)
120+
})
121+
return { promise, resolve: resolveA }
122+
}
123+
124+
const getResolve = async function(resolve) {
125+
resolve()
126+
await pNextTick()
127+
}
128+
83129
/* eslint-enable max-nested-callbacks */

0 commit comments

Comments
 (0)