jestjs/jest · error · Error

Cannot find `global.gc` function. Please run node with `--ex

Error message

Cannot find `global.gc` function. Please run node with `--expose-gc` and report this issue in jest repo.

What it means

collectHandles uses garbage collection to detect async handles keeping the process alive after a test. runGC first checks for globalThis.gc; if absent it tries to enable it via v8 flags and vm.runInNewContext('gc'). If gc is still unavailable after that, this error is thrown instructing the user to run Node with --expose-gc. It is a last-resort diagnostic for an unsupported or locked-down Node environment.

Source

Thrown at packages/jest-core/src/collectHandles.ts:53

  }

  return false;
}

const alwaysActive = () => true;

const hasWeakRef = typeof WeakRef === 'function';

const asyncSleep = promisify(setTimeout);

let gcFunc: (() => void) | undefined = (globalThis as any).gc;
function runGC() {
  if (!gcFunc) {
    v8.setFlagsFromString('--expose-gc');
    gcFunc = vm.runInNewContext('gc');
    v8.setFlagsFromString('--no-expose-gc');
    if (!gcFunc) {
      throw new Error(
        'Cannot find `global.gc` function. Please run node with `--expose-gc` and report this issue in jest repo.',
      );
    }
  }

  gcFunc();
}

// Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js
// Extracted as we want to format the result ourselves
export default function collectHandles(): HandleCollectionResult {
  const activeHandles = new Map<
    number,
    {error: Error; isActive: () => boolean}
  >();
  const hook = asyncHooks.createHook({
    destroy(asyncId) {
      activeHandles.delete(asyncId);

View on GitHub (pinned to f49721c78e)

Solutions

  1. Run Node with --expose-gc: node --expose-gc node_modules/.bin/jest
  2. Upgrade to a standard Node.js distribution
  3. If this persists on a standard Node, report the issue to the Jest repo as the message suggests

Example fix

# before
jest
# after
node --expose-gc node_modules/.bin/jest
Defensive patterns

Strategy: validation

Validate before calling

if (typeof (globalThis as any).gc !== 'function') {
  console.warn('Run node with --expose-gc for open-handles detection');
}

Prevention

When it happens

Trigger: Running Jest in an environment where --expose-gc cannot be enabled (some sandboxed/embedded Node runtimes, restricted containers), or an unusually locked-down Node build.

Common situations: Custom Node distributions without the v8 flag surface; very old or stripped Node builds; security-hardened sandboxes blocking setFlagsFromString.

Related errors


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