vitest-dev/vitest · error · Error

[@vitest/mocker] `createMockInstance` is not defined. This i

Error message

[@vitest/mocker] `createMockInstance` is not defined. This is a Vitest error. Please open a new issue with reproduction.

What it means

Thrown at packages/mocker/src/automocker.ts:36 when mockObject encounters a function-typed export and options.createMockInstance is undefined. The createMockInstance procedure is supplied by the host (ModuleMocker in browser/mocker.ts:155) and is responsible for producing a mock function instance. Its absence means the mocker was constructed or invoked without the procedure wired in, which is an internal contract violation rather than a user API mistake.

Source

Thrown at packages/mocker/src/automocker.ts:36

  object: Record<Key, any>,
  mockExports: Record<Key, any> = {},
): Record<Key, any> {
  const finalizers = new Array<() => void>()
  const refs = new RefTracker()

  const define = (container: Record<Key, any>, key: Key, value: any) => {
    try {
      container[key] = value
      return true
    }
    catch {
      return false
    }
  }

  const createMock = (currentValue: (...args: any[]) => any) => {
    if (!options.createMockInstance) {
      throw new Error(
        '[@vitest/mocker] `createMockInstance` is not defined. This is a Vitest error. Please open a new issue with reproduction.',
      )
    }
    const createMockInstance = options.createMockInstance
    const prototypeMembers = currentValue.prototype
      ? collectFunctionProperties(currentValue.prototype)
      : []
    return createMockInstance({
      name: currentValue.name,
      prototypeMembers,
      originalImplementation: options.type === 'autospy' ? currentValue : undefined,
      keepMembersImplementation: options.type === 'autospy',
    })
  }

  const mockPropertiesOf = (
    container: Record<Key, any>,
    newContainer: Record<Key, any>,

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Ensure @vitest/mocker, vitest, and @vitest/spy are on compatible versions (reinstall with pnpm install --force).
  2. If using a custom environment, verify ModuleMocker is constructed with all four constructor args including createMockInstance (browser/mocker.ts:17-22).
  3. Open a Vitest issue with a minimal reproduction as the message requests, since this indicates an internal wiring failure.
Defensive patterns

Strategy: try-catch

Try / catch

// This is an internal Vitest error; user code cannot pre-validate it.
// Catch to surface a clearer message and report.
try {
  vi.mock('./someModule') // automock path that triggers createMockInstance
} catch (e) {
  if (e instanceof Error && /createMockInstance is not defined/.test(e.message)) {
    console.error('Vitest internal mocker init failure. Reinstall matching @vitest/* versions and report the issue.')
  }
  throw e
}

Prevention

When it happens

Trigger: Constructing ModuleMocker (or calling mockObject directly) without passing a createMockInstance procedure, then automocking/spying a module that exports a class or function whose prototype has methods. The createMock closure at automocker.ts:34 guards on options.createMockInstance before invoking it.

Common situations: Vitest version mismatch between @vitest/mocker and vitest packages; a custom test environment or third-party runner that instantiates ModuleMocker without the procedure; corrupted/partial install where the procedure binding was lost during a refactor.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/6bb546d1a4acd63a.json. Report an issue: GitHub.