jestjs/jest · error · TypeError

`jest.runAllImmediates()` is only available when using legac

Error message

`jest.runAllImmediates()` is only available when using legacy fake timers.

What it means

TypeError thrown by `jest.runAllImmediates()` when modern fake timers are active. `runAllImmediates` flushes `setImmediate` queues and exists ONLY on the legacy fake timers implementation — the inverse of the other timer errors.

Source

Thrown at packages/jest-runtime/src/internals/JestGlobals.ts:376

      isolateModulesAsync,
      mock,
      mocked,
      now: () => _getFakeTimers().now(),
      onGenerateMock,
      replaceProperty,
      requireActual: moduleName => this.requireActualBridge(from, moduleName),
      requireMock: moduleName => this.requireMockBridge(from, moduleName),
      resetAllMocks,
      resetModules,
      restoreAllMocks,
      retryTimes,
      runAllImmediates: () => {
        const fakeTimers = _getFakeTimers();

        if (fakeTimers === this.environment.fakeTimers) {
          fakeTimers.runAllImmediates();
        } else {
          throw new TypeError(
            '`jest.runAllImmediates()` is only available when using legacy fake timers.',
          );
        }
      },
      runAllTicks: () => _getFakeTimers().runAllTicks(),
      runAllTimers: () => _getFakeTimers().runAllTimers(),
      runAllTimersAsync: async () => {
        const fakeTimers = _getFakeTimers();

        if (fakeTimers === this.environment.fakeTimersModern) {
          await fakeTimers.runAllTimersAsync();
        } else {
          throw new TypeError(
            '`jest.runAllTimersAsync()` is not available when using legacy fake timers.',
          );
        }
      },
      runOnlyPendingTimers: () => _getFakeTimers().runOnlyPendingTimers(),

View on GitHub (pinned to f49721c78e)

Solutions

  1. Switch to legacy fake timers: `jest.useFakeTimers({ legacyFakeTimers: true })` — but note legacy timers are deprecated.
  2. Preferably, replace `setImmediate`-based code with `queueMicrotask`/`Promise.resolve()` or use `jest.runAllTicks()` to flush microtasks under modern timers.
  3. If you specifically need to flush macrotasks, await a real `new Promise(r => setImmediate(r))` with real timers.

Example fix

// before
jest.useFakeTimers(); // modern
jest.runAllImmediates(); // throws

// after (option 1: legacy)
jest.useFakeTimers({ legacyFakeTimers: true });
jest.runAllImmediates();

// after (option 2: modern, flush microtasks instead)
jest.useFakeTimers();
jest.runAllTicks();
Defensive patterns

Strategy: validation

Validate before calling

// runAllImmediates needs legacy timers; modern tests should flush microtasks
function flushImmediateQueue() {
  // legacy path:
  // jest.useFakeTimers({ legacyFakeTimers: true }); jest.runAllImmediates();
  // modern equivalent:
  jest.runAllTicks();
}

Prevention

When it happens

Trigger: Calling `jest.runAllImmediates()` while modern fake timers are active (the default). The modern backend does not implement `setImmediate` flushing.

Common situations: Using the default modern timers in a test copied from an older Jest codebase that relied on `runAllImmediates`; testing code that uses `setImmediate` and expecting the legacy flush behavior.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/c537d7c1867ccbf8.json. Report an issue: GitHub.