{"record":{"id":"6bb546d1a4acd63a","repo":"vitest-dev/vitest","slug":"vitest-mocker-createmockinstance-is-not-defin","errorCode":null,"errorMessage":"[@vitest/mocker] `createMockInstance` is not defined. This is a Vitest error. Please open a new issue with reproduction.","messagePattern":"\\[@vitest/mocker\\] `createMockInstance` is not defined\\. This is a Vitest error\\. Please open a new issue with reproduction\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/automocker.ts","lineNumber":36,"sourceCode":"  object: Record<Key, any>,\n  mockExports: Record<Key, any> = {},\n): Record<Key, any> {\n  const finalizers = new Array<() => void>()\n  const refs = new RefTracker()\n\n  const define = (container: Record<Key, any>, key: Key, value: any) => {\n    try {\n      container[key] = value\n      return true\n    }\n    catch {\n      return false\n    }\n  }\n\n  const createMock = (currentValue: (...args: any[]) => any) => {\n    if (!options.createMockInstance) {\n      throw new Error(\n        '[@vitest/mocker] `createMockInstance` is not defined. This is a Vitest error. Please open a new issue with reproduction.',\n      )\n    }\n    const createMockInstance = options.createMockInstance\n    const prototypeMembers = currentValue.prototype\n      ? collectFunctionProperties(currentValue.prototype)\n      : []\n    return createMockInstance({\n      name: currentValue.name,\n      prototypeMembers,\n      originalImplementation: options.type === 'autospy' ? currentValue : undefined,\n      keepMembersImplementation: options.type === 'autospy',\n    })\n  }\n\n  const mockPropertiesOf = (\n    container: Record<Key, any>,\n    newContainer: Record<Key, any>,","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/automocker.ts#L18-L54","documentation":"`mockObject` needs a `createMockInstance` procedure supplied in its options to fabricate mock instances of class-like exports. If the caller did not provide one, the automocker cannot generate a mock and throws a plain `Error` asking for a bug report — this path is internal and should always be wired by Vitest.","triggerScenarios":"An automock/spy of a module that contains a class export where Vitest failed to inject `createMockInstance`; calling `mockObject` directly with incomplete options; a version mismatch where the runner does not pass the procedure.","commonSituations":"Upgrading Vitest and hitting an internal wiring regression; using the `@vitest/mocker` package standalone without providing `createMockInstance`; mocking a module whose export is a class in an environment Vitest does not fully support.","solutions":["Update Vitest to the latest patch — this is flagged as a Vitest internal error.","If calling `mockObject` directly, pass `createMockInstance` in options.","Switch from `vi.mock` automock to an explicit factory to avoid the class-mocking path.","Open an issue with a minimal reproduction as the message requests."],"exampleFix":"// before\nvi.mock('some-module-with-class') // triggers automocker path\n\n// after\nvi.mock('some-module-with-class', () => ({\n  MyClass: class { method() {} },\n}))","handlingStrategy":"try-catch","validationCode":"if (typeof options.createMockInstance !== 'function') {\n  throw new Error('createMockInstance is required for class automocking')\n}","typeGuard":"const hasCreateMockInstance = (\n  o: { createMockInstance?: unknown },\n): o is { createMockInstance: (...args: any[]) => any } =>\n  typeof o.createMockInstance === 'function'","tryCatchPattern":"try {\n  vi.mock('./module-with-class')\n} catch (err) {\n  // fall back to an explicit factory to avoid the automock class path\n  vi.mock('./module-with-class', () => ({ MyClass: class { method() {} } }))\n}","preventionTips":["Keep Vitest on the latest patch release.","Provide an explicit mock factory for modules exporting classes.","Do not call mockObject directly without supplying createMockInstance."],"tags":["mocker","automock","internal","vi-mock","vitest-bug"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}