From 46c9c13811ca20a887e2826c03852f2ccdebda7a Mon Sep 17 00:00:00 2001 From: Minijus L <3633549+minijus@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:02:33 +0300 Subject: [PATCH] fix: correct `instanceof` for `ModernFakeTimers` and `LegacyFakeTimers` methods (#11946) --- CHANGELOG.md | 1 + packages/jest-runtime/package.json | 2 +- packages/jest-runtime/src/index.ts | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b706156bc4b3..3a5f8474efbb 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]` Fix `instanceof` for `ModernFakeTimers` and `LegacyFakeTimers` methods ([#11946](https://github.com/facebook/jest/pull/11946)) ### Chore & Maintenance 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", diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c8cfa5c17246..8640b45ea405 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 (fakeTimers === this._environment.fakeTimersModern) { return fakeTimers.getRealSystemTime(); } else { throw new TypeError( @@ -1984,7 +1984,7 @@ export default class Runtime { runAllImmediates: () => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof LegacyFakeTimers) { + 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 (fakeTimers instanceof ModernFakeTimers) { + if (fakeTimers === this._environment.fakeTimersModern) { fakeTimers.setSystemTime(now); } else { throw new TypeError(