@@ -6,43 +6,47 @@ import { stub } from 'sinon'
6
6
7
7
import { DEFAULT_TIMEOUT , INFINITE_TIMEOUT , NO_TIMEOUT } from '../timeout.js'
8
8
9
- // eslint-disable-next-line no-restricted-globals
10
- stub ( console , 'error' )
11
- stub ( process , 'exit' )
12
- const clock = install ( )
13
-
14
9
// `handle-cli-error` use global variables `process.exitCode`, `process.exit()`
15
10
// and `console.error()` so we need to mock them.
16
11
// It also relies on timeout, which we need to mock as well.
17
12
export const handleError = ( error , options ) => {
13
+ const clock = startMocks ( )
14
+
18
15
try {
19
- resetMocks ( )
20
16
handleCliError ( error , options )
21
17
// eslint-disable-next-line no-restricted-globals, no-console
22
18
const consoleArg = getStubArg ( console . error )
23
- const processExitArgs = getProcessExitArgs ( options )
19
+ const processExitArgs = getProcessExitArgs ( clock , options )
24
20
return { consoleArg, ...processExitArgs }
25
21
} finally {
26
- process . exitCode = undefined
22
+ stopMocks ( clock )
27
23
}
28
24
}
29
25
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
31
37
// eslint-disable-next-line no-restricted-globals, no-console
32
- console . error . resetHistory ( )
33
- process . exit . resetHistory ( )
34
- clock . reset ( )
38
+ console . error . restore ( )
35
39
}
36
40
37
- const getProcessExitArgs = ( options ) => {
41
+ const getProcessExitArgs = ( clock , options ) => {
38
42
const { exitCode, exit } = process
39
43
const exitCodeBefore = getStubArg ( exit )
40
- advanceTimeout ( options )
44
+ advanceTimeout ( clock , options )
41
45
const exitCodeAfter = getStubArg ( exit )
42
46
return { exitCode, exitCodeBefore, exitCodeAfter }
43
47
}
44
48
45
- const advanceTimeout = ( { timeout = DEFAULT_TIMEOUT } = { } ) => {
49
+ const advanceTimeout = ( clock , { timeout = DEFAULT_TIMEOUT } = { } ) => {
46
50
if ( timeout !== NO_TIMEOUT && timeout !== INFINITE_TIMEOUT && timeout >= 0 ) {
47
51
clock . tick ( timeout )
48
52
}
0 commit comments