Skip to content

Commit f7b5c33

Browse files
authored
fix: Don't assume mocked timers imply jest fake timers (#900)
* fix: Don't assume mocked timers imply jest fake timers * Create dedicated jest timer functions The naming should indicate that they should only be called in a jest-like environment * No implicit return * I don't know how istanbul works and I don't care
1 parent a25149d commit f7b5c33

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/helpers.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ 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-
return _runWithRealTimers(callback).callbackReturnValue
8+
// istanbul ignore else
9+
if (typeof jest !== 'undefined') {
10+
return runWithJestRealTimers(callback).callbackReturnValue
11+
}
12+
13+
// istanbul ignore next
14+
return callback()
915
}
1016

11-
function _runWithRealTimers(callback) {
17+
function runWithJestRealTimers(callback) {
1218
const timerAPI = {
1319
clearImmediate,
1420
clearInterval,
@@ -18,29 +24,33 @@ function _runWithRealTimers(callback) {
1824
setTimeout,
1925
}
2026

21-
// istanbul ignore else
22-
if (typeof jest !== 'undefined') {
23-
jest.useRealTimers()
24-
}
27+
jest.useRealTimers()
2528

2629
const callbackReturnValue = callback()
2730

28-
const usedJestFakeTimers = Object.entries(timerAPI).some(
31+
const usedFakeTimers = Object.entries(timerAPI).some(
2932
([name, func]) => func !== globalObj[name],
3033
)
3134

32-
if (usedJestFakeTimers) {
35+
if (usedFakeTimers) {
3336
jest.useFakeTimers(timerAPI.setTimeout?.clock ? 'modern' : 'legacy')
3437
}
3538

3639
return {
3740
callbackReturnValue,
38-
usedJestFakeTimers,
41+
usedFakeTimers,
3942
}
4043
}
4144

42-
const jestFakeTimersAreEnabled = () =>
43-
Boolean(_runWithRealTimers(() => {}).usedJestFakeTimers)
45+
function jestFakeTimersAreEnabled() {
46+
// istanbul ignore else
47+
if (typeof jest !== 'undefined') {
48+
return runWithJestRealTimers(() => {}).usedFakeTimers
49+
}
50+
51+
// istanbul ignore next
52+
return false
53+
}
4454

4555
// we only run our tests in node, and setImmediate is supported in node.
4656
// istanbul ignore next

src/wait-for.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function waitFor(
5252

5353
const overallTimeoutTimer = setTimeout(handleTimeout, timeout)
5454

55-
const usingFakeTimers = jestFakeTimersAreEnabled()
56-
if (usingFakeTimers) {
55+
const usingJestFakeTimers = jestFakeTimersAreEnabled()
56+
if (usingJestFakeTimers) {
5757
checkCallback()
5858
// this is a dangerous rule to disable because it could lead to an
5959
// infinite loop. However, eslint isn't smart enough to know that we're
@@ -107,7 +107,7 @@ function waitFor(
107107
finished = true
108108
clearTimeout(overallTimeoutTimer)
109109

110-
if (!usingFakeTimers) {
110+
if (!usingJestFakeTimers) {
111111
clearInterval(intervalId)
112112
observer.disconnect()
113113
}

0 commit comments

Comments
 (0)