jestjs/jest · error · Error
${callerName} cannot be nested inside another ${callerName}
Error message
${callerName} cannot be nested inside another ${callerName} or ${other}. What it means
Thrown by `ModuleRegistries.enterIsolated` when `jest.isolateModules` (or `isolateModulesAsync`) is invoked while another isolation scope is already active. Jest's module registries support a single layer of isolation overlay (ModuleRegistries.ts:143-153); nesting would silently corrupt module resolution so the runtime hard-fails instead.
Source
Thrown at packages/jest-runtime/src/internals/ModuleRegistries.ts:149
getInternalCjsRegistry(): ModuleRegistry {
return this.internalModuleRegistry;
}
getActiveMockRegistry(): Map<string, unknown> {
return this.isolation?.mock ?? this.mockRegistry;
}
isIsolated(): boolean {
return this.isolation !== null;
}
enterIsolated(callerName: 'isolateModules' | 'isolateModulesAsync'): void {
if (this.isIsolated()) {
const other =
callerName === 'isolateModules'
? 'isolateModulesAsync'
: 'isolateModules';
throw new Error(
`${callerName} cannot be nested inside another ${callerName} or ${other}.`,
);
}
this.isolation = new Isolation();
}
exitIsolated(): void {
this.isolation?.clear();
this.isolation = null;
}
// Loads `fn` against fresh CJS + mock registries, then restores the
// originals. Used by `_generateMock` to keep automock loading from
// polluting the real caches.
withScratchRegistries<T>(fn: () => T): T {
const originalMock = this.mockRegistry;
const originalModule = this.moduleRegistry;
this.mockRegistry = new Map();View on GitHub (pinned to f49721c78e)
Solutions
- Flatten the nesting: keep a single `jest.isolateModules` (or `isolateModulesAsync`) call and do all the per-module `require` work inside it.
- If you need two distinct module states, run them sequentially in separate `isolateModules` blocks rather than nested.
- Audit helper functions for hidden `isolateModules` wrappers and remove the inner one.
- Switch the outer call to `jest.isolateModulesAsync` if you need `await` inside, and don't re-enter from the callback.
Example fix
// before
jest.isolateModules(() => {
const a = require('mod');
jest.isolateModules(() => {
const b = require('mod'); // throws
});
});
// after — sequential, not nested
jest.isolateModules(() => { const a = require('mod'); });
jest.isolateModules(() => { const b = require('mod'); }); Defensive patterns
Strategy: validation
Validate before calling
let isolationDepth = 0;
function safeIsolate(fn: () => void) {
if (isolationDepth > 0) {
throw new Error('Refusing to nest isolateModules');
}
isolationDepth++;
try {
jest.isolateModules(fn);
} finally {
isolationDepth--;
}
} Prevention
- Centralize all isolateModules usage behind a single helper so nested calls are caught at the helper boundary.
- Never put isolateModules inside a helper that itself is called from inside isolateModules.
- Prefer sequential isolated blocks over nested ones when you need multiple module states.
When it happens
Trigger: Calling `jest.isolateModules(() => { jest.isolateModules(() => { ... }) })`. Also when an `isolateModulesAsync` callback body awaits code that itself calls `isolateModules`, or vice versa (the message names both).
Common situations: Test helpers that wrap `require` in `isolateModules` invoked from inside another test helper already wrapping in `isolateModules`. Refactoring shared setup without realizing both layers isolate.
Related errors
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/cf8a7d71d72f69e4.json.
Report an issue: GitHub.