Skip to content

Commit 40616d8

Browse files
committed
Refactoring
1 parent 1f07237 commit 40616d8

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

test/exit.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,16 @@ import { EXIT_TIMEOUT, EXIT_CODE } from '../src/exit.js'
1010
import { emit } from './helpers/events.js'
1111
import {
1212
stubProcessClock,
13-
stubProcessExit,
1413
unStubProcessClock,
15-
unStubProcessExit,
14+
startExitLogging,
15+
startProcessLogging,
1616
} from './helpers/exit.js'
17-
import { setProcessEvent, unsetProcessEvent } from './helpers/process.js'
1817
import { removeProcessListeners } from './helpers/remove.js'
1918
import { startLogging } from './helpers/start.js'
2019

2120
const pNextTick = promisify(nextTick)
2221
removeProcessListeners()
2322

24-
const startProcessLogging = function (eventName, opts) {
25-
const processHandler = setProcessEvent(eventName)
26-
const stopLogging = startExitLogging(opts)
27-
return stopProcessLogging.bind(
28-
undefined,
29-
eventName,
30-
stopLogging,
31-
processHandler,
32-
)
33-
}
34-
35-
const stopProcessLogging = function (eventName, stopLogging, processHandler) {
36-
stopLogging()
37-
unsetProcessEvent(eventName, processHandler)
38-
}
39-
40-
const startExitLogging = function (opts) {
41-
stubProcessExit()
42-
return stopExitLogging.bind(undefined, startLogging(opts).stopLogging)
43-
}
44-
45-
const stopExitLogging = function (stopLogging) {
46-
stopLogging()
47-
unStubProcessExit()
48-
}
49-
5023
test.serial('call process.exit() after a timeout', async (t) => {
5124
const clock = stubProcessClock()
5225
const { stopLogging } = startLogging({ exit: true })

test/helpers/exit.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ import process from 'process'
33
import fakeTimers from '@sinonjs/fake-timers'
44
import sinon from 'sinon'
55

6+
import { setProcessEvent, unsetProcessEvent } from './process.js'
7+
import { startLogging } from './start.js'
8+
9+
// Start logging while a specific process event handler is being used, and
10+
// `process.exit()` is being stubbed
11+
export const startProcessLogging = function (eventName, opts) {
12+
const processHandler = setProcessEvent(eventName)
13+
const stopLogging = startExitLogging(opts)
14+
return stopProcessLogging.bind(
15+
undefined,
16+
eventName,
17+
stopLogging,
18+
processHandler,
19+
)
20+
}
21+
22+
const stopProcessLogging = function (eventName, stopLogging, processHandler) {
23+
stopLogging()
24+
unsetProcessEvent(eventName, processHandler)
25+
}
26+
27+
// Start logging while `process.exit()` is being stubbed
28+
export const startExitLogging = function (opts) {
29+
stubProcessExit()
30+
return stopExitLogging.bind(undefined, startLogging(opts).stopLogging)
31+
}
32+
33+
const stopExitLogging = function (stopLogging) {
34+
stopLogging()
35+
unStubProcessExit()
36+
}
37+
638
// Stub `setTimeout()` and `process.exit()`
739
export const stubProcessClock = function () {
840
stubProcessExit()

0 commit comments

Comments
 (0)