Skip to content

Commit 3936cde

Browse files
committed
Refactoring
1 parent 6282673 commit 3936cde

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

test/exit.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,29 @@ import { EXIT_TIMEOUT, EXIT_CODE } from '../src/exit.js'
99

1010
import { emit } from './helpers/events.js'
1111
import {
12-
stubProcessClock,
13-
unStubProcessClock,
12+
startClockLogging,
1413
startExitLogging,
1514
startProcessLogging,
1615
} from './helpers/exit.js'
1716
import { removeProcessListeners } from './helpers/remove.js'
18-
import { startLogging } from './helpers/start.js'
1917

2018
const pNextTick = promisify(nextTick)
2119
removeProcessListeners()
2220

2321
test.serial('call process.exit() after a timeout', async (t) => {
24-
const clock = stubProcessClock()
25-
const { stopLogging } = startLogging({ exit: true })
22+
const { clock, stopLogging } = startClockLogging({ exit: true })
2623

2724
await emit('uncaughtException')
2825
t.deepEqual(process.exit.args, [])
2926
clock.tick(EXIT_TIMEOUT)
3027
t.deepEqual(process.exit.args, [[EXIT_CODE]])
3128

3229
stopLogging()
33-
unStubProcessClock(clock)
3430
})
3531

36-
// eslint-disable-next-line max-statements
3732
test.serial('wait for async onError() before exiting', async (t) => {
38-
const clock = stubProcessClock()
3933
const onErrorDuration = 1e5
40-
const { stopLogging } = startLogging({
34+
const { clock, stopLogging } = startClockLogging({
4135
async onError() {
4236
await promisify(setTimeout)(onErrorDuration)
4337
},
@@ -52,7 +46,6 @@ test.serial('wait for async onError() before exiting', async (t) => {
5246
t.deepEqual(process.exit.args, [[EXIT_CODE]])
5347

5448
stopLogging()
55-
unStubProcessClock(clock)
5649
})
5750

5851
test.serial('exit process if "exit: true"', async (t) => {

test/helpers/exit.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ const stopExitLogging = function (stopLogging) {
3535
unStubProcessExit()
3636
}
3737

38+
// Start logging while `setTimeout()` and `process.exit()` are being stubbed
39+
export const startClockLogging = function (opts) {
40+
const clock = stubProcessClock()
41+
const { stopLogging } = startLogging(opts)
42+
const stopLoggingA = stopClockLogging.bind(undefined, stopLogging, clock)
43+
return { clock, stopLogging: stopLoggingA }
44+
}
45+
46+
const stopClockLogging = function (stopLogging, clock) {
47+
stopLogging()
48+
unStubProcessClock(clock)
49+
}
50+
3851
// Stub `setTimeout()` and `process.exit()`
39-
export const stubProcessClock = function () {
52+
const stubProcessClock = function () {
4053
stubProcessExit()
4154
return fakeTimers.install({ toFake: ['setTimeout'] })
4255
}
4356

44-
export const unStubProcessClock = function (clock) {
57+
const unStubProcessClock = function (clock) {
4558
unStubProcessExit()
4659
clock.uninstall()
4760
}

0 commit comments

Comments
 (0)