From de211da969c695973d877d8f26612742cb722d09 Mon Sep 17 00:00:00 2001 From: minijus <3633549+minijus@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:44:21 +0300 Subject: [PATCH 1/4] fix: use typeof type guard instead of instanceof to differentiate ModernFakeTimers and LegacyFakeTimers methods Closes #11660 #11767 #11662 --- CHANGELOG.md | 1 + packages/jest-runtime/src/index.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b706156bc4b3..5a125dcf0f58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943)) +- `[jest-runtime]` Use typeof type guard instead of instanceof to differentiate ModernFakeTimers and LegacyFakeTimers methods ([#11946](https://github.com/facebook/jest/pull/11946)) ### Chore & Maintenance diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c8cfa5c17246..b55ec85b4e77 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -30,7 +30,7 @@ import type { Module, ModuleWrapper, } from '@jest/environment'; -import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers'; +import type {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers'; import type * as JestGlobals from '@jest/globals'; import type {SourceMapRegistry} from '@jest/source-map'; import type {RuntimeTransformResult, V8CoverageResult} from '@jest/test-result'; @@ -1963,7 +1963,7 @@ export default class Runtime { getRealSystemTime: () => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof ModernFakeTimers) { + if (this._areModernFakeTimers(fakeTimers)) { return fakeTimers.getRealSystemTime(); } else { throw new TypeError( @@ -1984,7 +1984,7 @@ export default class Runtime { runAllImmediates: () => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof LegacyFakeTimers) { + if (!this._areModernFakeTimers(fakeTimers)) { fakeTimers.runAllImmediates(); } else { throw new TypeError( @@ -2000,7 +2000,7 @@ export default class Runtime { setSystemTime: (now?: number | Date) => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof ModernFakeTimers) { + if (this._areModernFakeTimers(fakeTimers)) { fakeTimers.setSystemTime(now); } else { throw new TypeError( @@ -2018,6 +2018,12 @@ export default class Runtime { return jestObject; } + private _areModernFakeTimers( + fakeTimers: ModernFakeTimers | LegacyFakeTimers, + ): fakeTimers is ModernFakeTimers { + return typeof (fakeTimers as ModernFakeTimers).setSystemTime === 'function'; + } + private _logFormattedReferenceError(errorMessage: string) { const testPath = this._testPath ? ` From ${slash(path.relative(this._config.rootDir, this._testPath))}.` From f1602d06558fa15da84b2a6219e925af7adc957d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Oct 2021 11:08:20 +0200 Subject: [PATCH 2/4] only dev dep now --- packages/jest-runtime/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index 881360f1050c..a498152b1a2e 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -16,7 +16,6 @@ "dependencies": { "@jest/console": "^27.2.5", "@jest/environment": "^27.2.5", - "@jest/fake-timers": "^27.2.5", "@jest/globals": "^27.2.5", "@jest/source-map": "^27.0.6", "@jest/test-result": "^27.2.5", @@ -43,6 +42,7 @@ "yargs": "^16.2.0" }, "devDependencies": { + "@jest/fake-timers": "^27.2.5", "@jest/test-utils": "^27.2.5", "@types/exit": "^0.1.30", "@types/glob": "^7.1.1", From e4b286742316e14b58d80e7a71f0192a799c4857 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Oct 2021 11:11:03 +0200 Subject: [PATCH 3/4] simplify check --- packages/jest-runtime/src/index.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b55ec85b4e77..8640b45ea405 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1963,7 +1963,7 @@ export default class Runtime { getRealSystemTime: () => { const fakeTimers = _getFakeTimers(); - if (this._areModernFakeTimers(fakeTimers)) { + if (fakeTimers === this._environment.fakeTimersModern) { return fakeTimers.getRealSystemTime(); } else { throw new TypeError( @@ -1984,7 +1984,7 @@ export default class Runtime { runAllImmediates: () => { const fakeTimers = _getFakeTimers(); - if (!this._areModernFakeTimers(fakeTimers)) { + if (fakeTimers === this._environment.fakeTimers) { fakeTimers.runAllImmediates(); } else { throw new TypeError( @@ -2000,7 +2000,7 @@ export default class Runtime { setSystemTime: (now?: number | Date) => { const fakeTimers = _getFakeTimers(); - if (this._areModernFakeTimers(fakeTimers)) { + if (fakeTimers === this._environment.fakeTimersModern) { fakeTimers.setSystemTime(now); } else { throw new TypeError( @@ -2018,12 +2018,6 @@ export default class Runtime { return jestObject; } - private _areModernFakeTimers( - fakeTimers: ModernFakeTimers | LegacyFakeTimers, - ): fakeTimers is ModernFakeTimers { - return typeof (fakeTimers as ModernFakeTimers).setSystemTime === 'function'; - } - private _logFormattedReferenceError(errorMessage: string) { const testPath = this._testPath ? ` From ${slash(path.relative(this._config.rootDir, this._testPath))}.` From e1ec9b75889c9315dbfb26bcfcd917fe617b789b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Oct 2021 11:12:27 +0200 Subject: [PATCH 4/4] tweak changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a125dcf0f58..3a5f8474efbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Fixes - `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943)) -- `[jest-runtime]` Use typeof type guard instead of instanceof to differentiate ModernFakeTimers and LegacyFakeTimers methods ([#11946](https://github.com/facebook/jest/pull/11946)) +- `[jest-runtime]` Fix `instanceof` for `ModernFakeTimers` and `LegacyFakeTimers` methods ([#11946](https://github.com/facebook/jest/pull/11946)) ### Chore & Maintenance