{"id":"24ee3ebb283de168","repo":"jestjs/jest","slug":"attempting-to-import-a-mock-without-a-factory","errorCode":null,"errorMessage":"Attempting to import a mock without a factory","messagePattern":"Attempting to import a mock without a factory","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/EsmLoader.ts","lineNumber":1324,"sourceCode":"\n  private async importMock<T = unknown>(\n    moduleName: string,\n    moduleID: string,\n    context: VMContext,\n  ): Promise<T> {\n    if (this.registries.hasModuleMock(moduleID)) {\n      return this.registries.getModuleMock(moduleID) as T;\n    }\n\n    const factory = this.mockState.getEsmFactory(moduleID);\n    if (factory) {\n      const invokedFactory = (await factory()) as Record<string, unknown>;\n      const module = syntheticFromExports(moduleName, context, invokedFactory);\n      this.registries.setModuleMock(moduleID, module);\n      return evaluateSyntheticModule(module) as T;\n    }\n\n    throw new Error('Attempting to import a mock without a factory');\n  }\n\n  private async importWasmModule(\n    source: BufferSource,\n    identifier: string,\n    context: VMContext,\n  ): Promise<SyntheticModule> {\n    // Use async `WebAssembly.compile` (rather than the sync constructor used\n    // by the v24.9+ sync core) to avoid blocking the event loop on large wasm\n    // modules in the legacy async path.\n    const wasmModule = await WebAssembly.compile(source);\n    const moduleLookup: Record<string, VMModule> = {};\n    for (const {module} of WebAssembly.Module.imports(wasmModule)) {\n      if (moduleLookup[module] === undefined) {\n        const resolvedModule = await this.resolveModule<VMModule>(\n          module,\n          identifier,\n          context,","sourceCodeStart":1306,"sourceCodeEnd":1342,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/EsmLoader.ts#L1306-L1342","documentation":"Thrown by EsmLoader.importMock when an ESM import resolves to a mocked module but no factory was registered for it via `jest.unstable_mockModule`. The async ESM mocking path requires a factory to synthesize the mock module; without one Jest cannot construct it.","triggerScenarios":"Calling `jest.mock('x')` (CJS-style, no factory) on a module that is later imported as ESM, or calling `jest.unstable_mockModule('x')` without a factory argument. The ESM loader looks up `mockState.getEsmFactory(moduleID)`, finds nothing, and throws.","commonSituations":"Mixing CJS `jest.mock` automocking with ESM imports (automocking is not supported for ESM — you must provide an explicit factory); forgetting the second argument to `unstable_mockModule`; a setup file calling `jest.mock` for a module that the test imports via ESM.","solutions":["Use `jest.unstable_mockModule('x', () => ({ ... }))` with an explicit factory function returning the mock namespace.","Do not call the CJS `jest.mock('x')` (without factory) for modules imported as ESM; ESM automocking is unsupported.","Ensure the factory is registered before the import: call `unstable_mockModule` in setupFilesAfterEach or at the top of the test before the dynamic `import()`.","If you need to unmock, call `jest.unstable_unmockModule('x')` rather than leaving a stub without a factory."],"exampleFix":"// before\njest.mock('my-esm-module'); // CJS automock, no factory\nimport('my-esm-module'); // throws\n\n// after\njest.unstable_mockModule('my-esm-module', () => ({\n  doThing: () => 'mocked',\n}));\nawait import('my-esm-module');","handlingStrategy":"validation","validationCode":"// Before importing an ESM module you intend to mock, ensure a factory is registered\nfunction mockEsm(name, factory) {\n  if (typeof factory !== 'function') {\n    throw new Error(`unstable_mockModule('${name}') needs a factory function`);\n  }\n  jest.unstable_mockModule(name, factory);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a factory to `jest.unstable_mockModule` — never use bare `jest.mock` for ESM.","Register the mock factory before the dynamic `import()` of the target module.","Do not mix CJS `jest.mock` automocking with ESM imports of the same module."],"tags":["esm","mocking","factory","unstable-mockmodule"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}