{"id":"b06fb94342968dff","repo":"jestjs/jest","slug":"unstable-mockmodule-must-be-passed-a-mock-factor","errorCode":null,"errorMessage":"`unstable_mockModule` must be passed a mock factory","messagePattern":"`unstable_mockModule` must be passed a mock factory","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/JestGlobals.ts","lineNumber":209,"sourceCode":"    ) => {\n      this.mockState.addOnGenerateMock(cb);\n      return jestObject;\n    };\n    const setMockFactory = (\n      moduleName: string,\n      mockFactory: () => unknown,\n      options?: {virtual?: boolean},\n    ) => {\n      this.setMockBridge(from, moduleName, mockFactory, options);\n      return jestObject;\n    };\n    const mockModule: Jest['unstable_mockModule'] = (\n      moduleName,\n      mockFactory,\n      options,\n    ) => {\n      if (typeof mockFactory !== 'function') {\n        throw new TypeError(\n          '`unstable_mockModule` must be passed a mock factory',\n        );\n      }\n\n      this.setModuleMockBridge(from, moduleName, mockFactory, options);\n      return jestObject;\n    };\n    const clearAllMocks = () => {\n      this.clearAllMocksBridge();\n      return jestObject;\n    };\n    const resetAllMocks = () => {\n      this.resetAllMocksBridge();\n      return jestObject;\n    };\n    const restoreAllMocks = () => {\n      this.restoreAllMocksBridge();\n      return jestObject;","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/JestGlobals.ts#L191-L227","documentation":"TypeError thrown by the `jest.unstable_mockModule` implementation when the second argument (the mock factory) is not a function. ESM module mocking requires an explicit factory because automocking is not supported for ESM, so a non-function factory is rejected up front.","triggerScenarios":"Calling `jest.unstable_mockModule('x', factory)` where `factory` is undefined, null, an object, or any non-function value. Common when the caller forgets the second argument or passes the mock object directly instead of a function returning it.","commonSituations":"Forgetting the factory argument entirely; passing `{ doThing: fn }` (the exports object) instead of `() => ({ doThing: fn })` (a factory returning it); migrating from `jest.mock('x', () => ({...}))` and dropping the arrow by mistake.","solutions":["Pass a function as the second argument: `jest.unstable_mockModule('x', () => ({ ... }))`.","Double-check that the factory is an arrow or normal function, not a plain object.","If using TypeScript, the type signature should already flag this; ensure `@types/jest`/`@jest/globals` is up to date."],"exampleFix":"// before\njest.unstable_mockModule('my-module', { doThing: () => 'x' });\n\n// after\njest.unstable_mockModule('my-module', () => ({ doThing: () => 'x' }));","handlingStrategy":"type-guard","validationCode":"function mockModule(name, factory) {\n  if (typeof factory !== 'function') {\n    throw new TypeError('unstable_mockModule requires a factory function');\n  }\n  jest.unstable_mockModule(name, factory);\n}","typeGuard":"function isMockFactory(f: unknown): f is () => Record<string, unknown> {\n  return typeof f === 'function';\n}","tryCatchPattern":null,"preventionTips":["Always pass an arrow/normal function as the second argument, not a plain object.","Enable @jest/globals types so TypeScript flags a missing factory at compile time.","Wrap unstable_mockModule in a project-local helper that validates the factory type."],"tags":["esm","mocking","factory","validation","unstable-mockmodule"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}