@@ -5,10 +5,15 @@ const TEXT_NODE = 3
5
5
6
6
// Currently this fn only supports jest timers, but it could support other test runners in the future.
7
7
function runWithRealTimers ( callback ) {
8
- return _runWithRealTimers ( callback ) . callbackReturnValue
8
+ // istanbul ignore else
9
+ if ( typeof jest !== 'undefined' ) {
10
+ return runWithJestRealTimers ( callback ) . callbackReturnValue
11
+ }
12
+
13
+ return callback ( )
9
14
}
10
15
11
- function _runWithRealTimers ( callback ) {
16
+ function runWithJestRealTimers ( callback ) {
12
17
const timerAPI = {
13
18
clearImmediate,
14
19
clearInterval,
@@ -18,29 +23,26 @@ function _runWithRealTimers(callback) {
18
23
setTimeout,
19
24
}
20
25
21
- // istanbul ignore else
22
- if ( typeof jest !== 'undefined' ) {
23
- jest . useRealTimers ( )
24
- }
26
+ jest . useRealTimers ( )
25
27
26
28
const callbackReturnValue = callback ( )
27
29
28
- const usedJestFakeTimers =
29
- typeof jest !== 'undefined' &&
30
- Object . entries ( timerAPI ) . some ( ( [ name , func ] ) => func !== globalObj [ name ] )
30
+ const usedFakeTimers = Object . entries ( timerAPI ) . some (
31
+ ( [ name , func ] ) => func !== globalObj [ name ] ,
32
+ )
31
33
32
- if ( usedJestFakeTimers ) {
34
+ if ( usedFakeTimers ) {
33
35
jest . useFakeTimers ( timerAPI . setTimeout ?. clock ? 'modern' : 'legacy' )
34
36
}
35
37
36
38
return {
37
39
callbackReturnValue,
38
- usedJestFakeTimers ,
40
+ usedFakeTimers ,
39
41
}
40
42
}
41
43
42
44
const jestFakeTimersAreEnabled = ( ) =>
43
- Boolean ( _runWithRealTimers ( ( ) => { } ) . usedJestFakeTimers )
45
+ typeof jest !== 'undefined' && runWithJestRealTimers ( ( ) => { } ) . usedFakeTimers
44
46
45
47
// we only run our tests in node, and setImmediate is supported in node.
46
48
// istanbul ignore next
0 commit comments