From 91c932ccba9cf776e1c8e551e13c63191fefe2f5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 Jun 2021 12:25:00 +0200 Subject: [PATCH] fix(legacy-timers): Do not add `setImmediate` and `clearImmediate` if they do not exist in the global environment --- CHANGELOG.md | 3 ++- .../jest-fake-timers/src/legacyFakeTimers.ts | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28bd375931e4..db8108c9dd3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,10 @@ ### Fixes +- `[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)) +- `[jest-core]` Support special characters like `@`, `+` and `()` on Windows with `--findRelatedTests` ([#11548](https://github.com/facebook/jest/pull/11548)) - `[jest-reporter]` Allow `node-notifier@10` as peer dependency ([#11523](https://github.com/facebook/jest/pull/11523)) - `[jest-reporter]` Update `v8-to-istanbul` ([#11523](https://github.com/facebook/jest/pull/11523)) -- `[jest-core]` Support special characters like `@`, `+` and `()` on windows with `--findRelatedTests` ([#11548](https://github.com/facebook/jest/pull/11548)) ### Chore & Maintenance diff --git a/packages/jest-fake-timers/src/legacyFakeTimers.ts b/packages/jest-fake-timers/src/legacyFakeTimers.ts index 34486ead5076..1805e8c9b9af 100644 --- a/packages/jest-fake-timers/src/legacyFakeTimers.ts +++ b/packages/jest-fake-timers/src/legacyFakeTimers.ts @@ -334,7 +334,9 @@ export default class FakeTimers { this._timerAPIs.cancelAnimationFrame, ); } - setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate); + if (typeof global.clearImmediate === 'function') { + setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate); + } setGlobal(global, 'clearInterval', this._timerAPIs.clearInterval); setGlobal(global, 'clearTimeout', this._timerAPIs.clearTimeout); if (typeof global.requestAnimationFrame === 'function') { @@ -344,7 +346,9 @@ export default class FakeTimers { this._timerAPIs.requestAnimationFrame, ); } - setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate); + if (typeof global.setImmediate === 'function') { + setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate); + } setGlobal(global, 'setInterval', this._timerAPIs.setInterval); setGlobal(global, 'setTimeout', this._timerAPIs.setTimeout); @@ -362,7 +366,9 @@ export default class FakeTimers { this._fakeTimerAPIs.cancelAnimationFrame, ); } - setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate); + if (typeof global.clearImmediate === 'function') { + setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate); + } setGlobal(global, 'clearInterval', this._fakeTimerAPIs.clearInterval); setGlobal(global, 'clearTimeout', this._fakeTimerAPIs.clearTimeout); if (typeof global.requestAnimationFrame === 'function') { @@ -372,7 +378,9 @@ export default class FakeTimers { this._fakeTimerAPIs.requestAnimationFrame, ); } - setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate); + if (typeof global.setImmediate === 'function') { + setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate); + } setGlobal(global, 'setInterval', this._fakeTimerAPIs.setInterval); setGlobal(global, 'setTimeout', this._fakeTimerAPIs.setTimeout);