{"id":"cf8a7d71d72f69e4","repo":"jestjs/jest","slug":"callername-cannot-be-nested-inside-another-ca","errorCode":null,"errorMessage":"${callerName} cannot be nested inside another ${callerName} or ${other}.","messagePattern":"(.+?) cannot be nested inside another (.+?) or (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/ModuleRegistries.ts","lineNumber":149,"sourceCode":"  getInternalCjsRegistry(): ModuleRegistry {\n    return this.internalModuleRegistry;\n  }\n\n  getActiveMockRegistry(): Map<string, unknown> {\n    return this.isolation?.mock ?? this.mockRegistry;\n  }\n\n  isIsolated(): boolean {\n    return this.isolation !== null;\n  }\n\n  enterIsolated(callerName: 'isolateModules' | 'isolateModulesAsync'): void {\n    if (this.isIsolated()) {\n      const other =\n        callerName === 'isolateModules'\n          ? 'isolateModulesAsync'\n          : 'isolateModules';\n      throw new Error(\n        `${callerName} cannot be nested inside another ${callerName} or ${other}.`,\n      );\n    }\n    this.isolation = new Isolation();\n  }\n\n  exitIsolated(): void {\n    this.isolation?.clear();\n    this.isolation = null;\n  }\n\n  // Loads `fn` against fresh CJS + mock registries, then restores the\n  // originals. Used by `_generateMock` to keep automock loading from\n  // polluting the real caches.\n  withScratchRegistries<T>(fn: () => T): T {\n    const originalMock = this.mockRegistry;\n    const originalModule = this.moduleRegistry;\n    this.mockRegistry = new Map();","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/ModuleRegistries.ts#L131-L167","documentation":"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.","triggerScenarios":"Calling `jest.isolateModules(() => { jest.isolateModules(() => { ... }) })`. Also when an `isolateModulesAsync` callback body awaits code that itself calls `isolateModules`, or vice versa (the message names both).","commonSituations":"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.","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."],"exampleFix":"// before\njest.isolateModules(() => {\n  const a = require('mod');\n  jest.isolateModules(() => {\n    const b = require('mod'); // throws\n  });\n});\n\n// after — sequential, not nested\njest.isolateModules(() => { const a = require('mod'); });\njest.isolateModules(() => { const b = require('mod'); });","handlingStrategy":"validation","validationCode":"let isolationDepth = 0;\nfunction safeIsolate(fn: () => void) {\n  if (isolationDepth > 0) {\n    throw new Error('Refusing to nest isolateModules');\n  }\n  isolationDepth++;\n  try {\n    jest.isolateModules(fn);\n  } finally {\n    isolationDepth--;\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["isolate-modules","module-registry","nesting"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}