Skip to content

Commit 6373529

Browse files
authored
fix(legacy-timers): Do not add setImmediate and clearImmediate if they do not exist in the global environment (#11599)
1 parent e5f9f21 commit 6373529

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
### Fixes
99

10+
- `[jest-fake-timers]` Do not add `setImmediate` and `clearImmediate` if they do not exist in the global environment ([#11599](https://github.com/facebook/jest/pull/11599))
11+
- `[jest-core]` Support special characters like `@`, `+` and `()` on Windows with `--findRelatedTests` ([#11548](https://github.com/facebook/jest/pull/11548))
1012
- `[jest-reporter]` Allow `node-notifier@10` as peer dependency ([#11523](https://github.com/facebook/jest/pull/11523))
1113
- `[jest-reporter]` Update `v8-to-istanbul` ([#11523](https://github.com/facebook/jest/pull/11523))
12-
- `[jest-core]` Support special characters like `@`, `+` and `()` on windows with `--findRelatedTests` ([#11548](https://github.com/facebook/jest/pull/11548))
1314

1415
### Chore & Maintenance
1516

packages/jest-fake-timers/src/legacyFakeTimers.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ export default class FakeTimers<TimerRef> {
334334
this._timerAPIs.cancelAnimationFrame,
335335
);
336336
}
337-
setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate);
337+
if (typeof global.clearImmediate === 'function') {
338+
setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate);
339+
}
338340
setGlobal(global, 'clearInterval', this._timerAPIs.clearInterval);
339341
setGlobal(global, 'clearTimeout', this._timerAPIs.clearTimeout);
340342
if (typeof global.requestAnimationFrame === 'function') {
@@ -344,7 +346,9 @@ export default class FakeTimers<TimerRef> {
344346
this._timerAPIs.requestAnimationFrame,
345347
);
346348
}
347-
setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate);
349+
if (typeof global.setImmediate === 'function') {
350+
setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate);
351+
}
348352
setGlobal(global, 'setInterval', this._timerAPIs.setInterval);
349353
setGlobal(global, 'setTimeout', this._timerAPIs.setTimeout);
350354

@@ -362,7 +366,9 @@ export default class FakeTimers<TimerRef> {
362366
this._fakeTimerAPIs.cancelAnimationFrame,
363367
);
364368
}
365-
setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate);
369+
if (typeof global.clearImmediate === 'function') {
370+
setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate);
371+
}
366372
setGlobal(global, 'clearInterval', this._fakeTimerAPIs.clearInterval);
367373
setGlobal(global, 'clearTimeout', this._fakeTimerAPIs.clearTimeout);
368374
if (typeof global.requestAnimationFrame === 'function') {
@@ -372,7 +378,9 @@ export default class FakeTimers<TimerRef> {
372378
this._fakeTimerAPIs.requestAnimationFrame,
373379
);
374380
}
375-
setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate);
381+
if (typeof global.setImmediate === 'function') {
382+
setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate);
383+
}
376384
setGlobal(global, 'setInterval', this._fakeTimerAPIs.setInterval);
377385
setGlobal(global, 'setTimeout', this._fakeTimerAPIs.setTimeout);
378386

0 commit comments

Comments
 (0)