vitest-dev/vitest · error · Error
[@vitest/mocker] `createMockInstance` is not defined. This…
Error message
[@vitest/mocker] `createMockInstance` is not defined. This is a Vitest error. Please open a new issue with reproduction.
What it means
`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.
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.
Example fix
// before
vi.mock('some-module-with-class') // triggers automocker path
// after
vi.mock('some-module-with-class', () => ({
MyClass: class { method() {} },
})) Defensive patterns
Strategy: try-catch
Validate before calling
if (typeof options.createMockInstance !== 'function') {
throw new Error('createMockInstance is required for class automocking')
} Type guard
const hasCreateMockInstance = (
o: { createMockInstance?: unknown },
): o is { createMockInstance: (...args: any[]) => any } =>
typeof o.createMockInstance === 'function' Try / catch
try {
vi.mock('./module-with-class')
} catch (err) {
// fall back to an explicit factory to avoid the automock class path
vi.mock('./module-with-class', () => ({ MyClass: class { method() {} } }))
} Prevention
- Keep Vitest on the latest patch release.
- Provide an explicit mock factory for modules exporting classes.
- Do not call mockObject directly without supplying createMockInstance.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- AssignmentPattern is not supported. Please open a new bug…
- automocking files with `export *` is not supported because…
- Cannot automock ' ' because it failed to parse.
- failed to parse
- MemberExpression is not supported. Please open a new bug…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/6bb546d1a4acd63a.
Report an issue: GitHub.
Appendix: 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 1fa9837ec2)