Skip to content

Commit d573eb2

Browse files
committed
Fix tests
1 parent 39c963c commit d573eb2

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/helpers/main.test.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,47 @@ import { stub } from 'sinon'
66

77
import { DEFAULT_TIMEOUT, INFINITE_TIMEOUT, NO_TIMEOUT } from '../timeout.js'
88

9-
// eslint-disable-next-line no-restricted-globals
10-
stub(console, 'error')
11-
stub(process, 'exit')
12-
const clock = install()
13-
149
// `handle-cli-error` use global variables `process.exitCode`, `process.exit()`
1510
// and `console.error()` so we need to mock them.
1611
// It also relies on timeout, which we need to mock as well.
1712
export const handleError = (error, options) => {
13+
const clock = startMocks()
14+
1815
try {
19-
resetMocks()
2016
handleCliError(error, options)
2117
// eslint-disable-next-line no-restricted-globals, no-console
2218
const consoleArg = getStubArg(console.error)
23-
const processExitArgs = getProcessExitArgs(options)
19+
const processExitArgs = getProcessExitArgs(clock, options)
2420
return { consoleArg, ...processExitArgs }
2521
} finally {
26-
process.exitCode = undefined
22+
stopMocks(clock)
2723
}
2824
}
2925

30-
const resetMocks = () => {
26+
const startMocks = () => {
27+
// eslint-disable-next-line no-restricted-globals
28+
stub(console, 'error')
29+
stub(process, 'exit')
30+
return install()
31+
}
32+
33+
const stopMocks = (clock) => {
34+
clock.uninstall()
35+
process.exit.restore()
36+
process.exitCode = undefined
3137
// eslint-disable-next-line no-restricted-globals, no-console
32-
console.error.resetHistory()
33-
process.exit.resetHistory()
34-
clock.reset()
38+
console.error.restore()
3539
}
3640

37-
const getProcessExitArgs = (options) => {
41+
const getProcessExitArgs = (clock, options) => {
3842
const { exitCode, exit } = process
3943
const exitCodeBefore = getStubArg(exit)
40-
advanceTimeout(options)
44+
advanceTimeout(clock, options)
4145
const exitCodeAfter = getStubArg(exit)
4246
return { exitCode, exitCodeBefore, exitCodeAfter }
4347
}
4448

45-
const advanceTimeout = ({ timeout = DEFAULT_TIMEOUT } = {}) => {
49+
const advanceTimeout = (clock, { timeout = DEFAULT_TIMEOUT } = {}) => {
4650
if (timeout !== NO_TIMEOUT && timeout !== INFINITE_TIMEOUT && timeout >= 0) {
4751
clock.tick(timeout)
4852
}

0 commit comments

Comments
 (0)