Skip to content

Commit 1b19094

Browse files
ph-fritscheeps1lon
andauthored
fix: Guard against jest.useRealTimers not existing (#934)
Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 793d598 commit 1b19094

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/helpers.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ const TEXT_NODE = 3
55

66
// Currently this fn only supports jest timers, but it could support other test runners in the future.
77
function runWithRealTimers(callback) {
8-
// istanbul ignore else
9-
if (typeof jest !== 'undefined') {
10-
return runWithJestRealTimers(callback).callbackReturnValue
11-
}
8+
return hasJestTimers()
9+
? runWithJestRealTimers(callback).callbackReturnValue
10+
: // istanbul ignore next
11+
callback()
12+
}
1213

13-
// istanbul ignore next
14-
return callback()
14+
function hasJestTimers() {
15+
return (
16+
typeof jest !== 'undefined' &&
17+
jest !== null &&
18+
typeof jest.useRealTimers === 'function'
19+
)
1520
}
1621

1722
function runWithJestRealTimers(callback) {
@@ -50,13 +55,10 @@ function runWithJestRealTimers(callback) {
5055
}
5156

5257
function jestFakeTimersAreEnabled() {
53-
// istanbul ignore else
54-
if (typeof jest !== 'undefined') {
55-
return runWithJestRealTimers(() => {}).usedFakeTimers
56-
}
57-
58-
// istanbul ignore next
59-
return false
58+
return hasJestTimers()
59+
? runWithJestRealTimers(() => {}).usedFakeTimers
60+
: // istanbul ignore next
61+
false
6062
}
6163

6264
// we only run our tests in node, and setImmediate is supported in node.

0 commit comments

Comments
 (0)