jestjs/jest · error · TypeError
`jest.setSystemTime()` is not available when using legacy fa
Error message
`jest.setSystemTime()` is not available when using legacy fake timers.
What it means
TypeError thrown by `jest.setSystemTime()` when legacy fake timers are active. Setting the fake system clock is a modern-fake-timers-only feature; the legacy implementation does not expose `setSystemTime`.
Source
Thrown at packages/jest-runtime/src/internals/JestGlobals.ts:413
runOnlyPendingTimersAsync: async () => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this.environment.fakeTimersModern) {
await fakeTimers.runOnlyPendingTimersAsync();
} else {
throw new TypeError(
'`jest.runOnlyPendingTimersAsync()` is not available when using legacy fake timers.',
);
}
},
setMock: (moduleName, mock) => setMockFactory(moduleName, () => mock),
setSystemTime: now => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this.environment.fakeTimersModern) {
fakeTimers.setSystemTime(now);
} else {
throw new TypeError(
'`jest.setSystemTime()` is not available when using legacy fake timers.',
);
}
},
setTimeout,
setTimerTickMode: (
mode:
| {mode: 'manual' | 'nextAsync'}
| {mode: 'interval'; delta?: number},
) => {
const fakeTimers = _getFakeTimers();
if (fakeTimers === this.environment.fakeTimersModern) {
fakeTimers.setTimerTickMode(mode);
} else {
throw new TypeError(
'`jest.setTimerTickMode()` is not available when using legacy fake timers.',
);
}View on GitHub (pinned to f49721c78e)
Solutions
- Switch to modern fake timers: `jest.useFakeTimers()` (default).
- If legacy timers are required, mock `Date` directly with `jest.spyOn(global, 'Date')` or use a library like `mockdate`, though migrating to modern timers is strongly preferred.
Example fix
// before
jest.useFakeTimers({ legacyFakeTimers: true });
jest.setSystemTime(new Date('2025-01-01')); // throws
// after
jest.useFakeTimers();
jest.setSystemTime(new Date('2025-01-01')); Defensive patterns
Strategy: validation
Validate before calling
// Modern timers support setSystemTime; legacy does not
jest.useFakeTimers(); // modern
jest.setSystemTime(new Date('2025-01-01')); Prevention
- Use modern fake timers for time-travel tests.
- Avoid legacyFakeTimers in config when tests set the system clock.
When it happens
Trigger: Calling `jest.setSystemTime(now)` while `legacyFakeTimers: true` is set.
Common situations: Time-dependent tests (e.g. asserting on `new Date()` output) where the project defaults to legacy timers; time-travel tests for date libraries.
Related errors
- `jest.getRealSystemTime()` is not available when using legac
- `jest.advanceTimersByTimeAsync()` is not available when usin
- `jest.advanceTimersToNextFrame()` is not available when usin
- `jest.advanceTimersToNextTimerAsync()` is not available when
- `jest.runAllTimersAsync()` is not available when using legac
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/23cb0ebaac7e80dd.json.
Report an issue: GitHub.