Skip to content

Commit de211da

Browse files
committed
fix: use typeof type guard instead of instanceof to differentiate ModernFakeTimers and LegacyFakeTimers methods
Closes #11660 #11767 #11662
1 parent 3674bbf commit de211da

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943))
8+
- `[jest-runtime]` Use typeof type guard instead of instanceof to differentiate ModernFakeTimers and LegacyFakeTimers methods ([#11946](https://github.com/facebook/jest/pull/11946))
89

910
### Chore & Maintenance
1011

packages/jest-runtime/src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
Module,
3131
ModuleWrapper,
3232
} from '@jest/environment';
33-
import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers';
33+
import type {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers';
3434
import type * as JestGlobals from '@jest/globals';
3535
import type {SourceMapRegistry} from '@jest/source-map';
3636
import type {RuntimeTransformResult, V8CoverageResult} from '@jest/test-result';
@@ -1963,7 +1963,7 @@ export default class Runtime {
19631963
getRealSystemTime: () => {
19641964
const fakeTimers = _getFakeTimers();
19651965

1966-
if (fakeTimers instanceof ModernFakeTimers) {
1966+
if (this._areModernFakeTimers(fakeTimers)) {
19671967
return fakeTimers.getRealSystemTime();
19681968
} else {
19691969
throw new TypeError(
@@ -1984,7 +1984,7 @@ export default class Runtime {
19841984
runAllImmediates: () => {
19851985
const fakeTimers = _getFakeTimers();
19861986

1987-
if (fakeTimers instanceof LegacyFakeTimers) {
1987+
if (!this._areModernFakeTimers(fakeTimers)) {
19881988
fakeTimers.runAllImmediates();
19891989
} else {
19901990
throw new TypeError(
@@ -2000,7 +2000,7 @@ export default class Runtime {
20002000
setSystemTime: (now?: number | Date) => {
20012001
const fakeTimers = _getFakeTimers();
20022002

2003-
if (fakeTimers instanceof ModernFakeTimers) {
2003+
if (this._areModernFakeTimers(fakeTimers)) {
20042004
fakeTimers.setSystemTime(now);
20052005
} else {
20062006
throw new TypeError(
@@ -2018,6 +2018,12 @@ export default class Runtime {
20182018
return jestObject;
20192019
}
20202020

2021+
private _areModernFakeTimers(
2022+
fakeTimers: ModernFakeTimers | LegacyFakeTimers<unknown>,
2023+
): fakeTimers is ModernFakeTimers {
2024+
return typeof (fakeTimers as ModernFakeTimers).setSystemTime === 'function';
2025+
}
2026+
20212027
private _logFormattedReferenceError(errorMessage: string) {
20222028
const testPath = this._testPath
20232029
? ` From ${slash(path.relative(this._config.rootDir, this._testPath))}.`

0 commit comments

Comments
 (0)