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
- Run Node with --expose-gc: node --expose-gc node_modules/.bin/jest
- Upgrade to a standard Node.js distribution
- 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
- Run the jest CLI under node --expose-gc in CI for accurate handle reporting
- Use a standard Node.js distribution
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
- jest.config.mts requires native TypeScript support. Ensure y
- For a percentage based memory limit a percentageReference mu
- Unexpected numerical input
- Unexpected input
- Custom export conditions specified but they are not an array
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/079b685a6ef10032.json.
Report an issue: GitHub.