diff --git a/CHANGELOG.md b/CHANGELOG.md index b706156bc4b3..3b352910144f 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` assertion 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 ffe5bc4a64aa..4c308010b30d 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'; @@ -1961,8 +1961,11 @@ export default class Runtime { getRealSystemTime: () => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof ModernFakeTimers) { - return fakeTimers.getRealSystemTime(); + if ( + typeof (fakeTimers as ModernFakeTimers).getRealSystemTime === + 'function' + ) { + return (fakeTimers as ModernFakeTimers).getRealSystemTime(); } else { throw new TypeError( 'getRealSystemTime is not available when not using modern timers', @@ -1982,8 +1985,11 @@ export default class Runtime { runAllImmediates: () => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof LegacyFakeTimers) { - fakeTimers.runAllImmediates(); + if ( + typeof (fakeTimers as LegacyFakeTimers).runAllImmediates === + 'function' + ) { + (fakeTimers as LegacyFakeTimers).runAllImmediates(); } else { throw new TypeError( 'runAllImmediates is not available when using modern timers', @@ -1998,8 +2004,10 @@ export default class Runtime { setSystemTime: (now?: number | Date) => { const fakeTimers = _getFakeTimers(); - if (fakeTimers instanceof ModernFakeTimers) { - fakeTimers.setSystemTime(now); + if ( + typeof (fakeTimers as ModernFakeTimers).setSystemTime === 'function' + ) { + (fakeTimers as ModernFakeTimers).setSystemTime(now); } else { throw new TypeError( 'setSystemTime is not available when not using modern timers',