vitest-dev/vitest · error · Error
importMock is not implemented
Error message
importMock is not implemented
What it means
Symmetric with `importActual`: `BareModuleMocker.importMock` (`bareModuleMocker.ts:292`) throws `"importMock is not implemented"` because the base class has no module loader. Only `NativeModuleMocker` implements `importMock` (resolving and loading the automocked/redirected module). Hitting this indicates the native mocker isn't active.
Source
Thrown at packages/vitest/src/runtime/moduleRunner/bareModuleMocker.ts:292
const redirect = this.findMockRedirect(id, external)
if (redirect) {
registry.register('redirect', originalId, id, url, redirect)
}
else {
registry.register('automock', originalId, id, url)
}
}
// every time the mock is registered, we remove the previous one from the cache
this.invalidateModuleById(id)
}
async importActual<T>(_rawId: string, _importer: string, _callstack?: string[] | null): Promise<T> {
throw new Error(`importActual is not implemented`)
}
async importMock<T>(_rawId: string, _importer: string, _callstack?: string[] | null): Promise<T> {
throw new Error(`importMock is not implemented`)
}
public queueMock(
id: string,
importer: string,
factoryOrOptions?: MockFactory | MockOptions,
): void {
const mockType = getMockType(factoryOrOptions)
BareModuleMocker.pendingIds.push({
action: 'mock',
id,
importer,
factory: typeof factoryOrOptions === 'function' ? factoryOrOptions : undefined,
type: mockType,
})
}
public queueUnmock(id: string, importer: string): void {View on GitHub (pinned to d568f8ce37)
Solutions
- Move the test to the Node environment (`// @vitest-environment node`).
- Avoid `vi.importMock` in browser contexts.
- Register the mock via `vi.mock` + factory and import the module normally instead.
Defensive patterns
Strategy: validation
Validate before calling
const isNode = typeof process !== 'undefined' && process.versions?.node
if (!isNode) {
throw new Error('vi.importMock is unavailable in this environment')
}
const mocked = await vi.importMock('mod') Try / catch
try {
return await vi.importMock('mod')
} catch (e) {
if (e instanceof Error && /importMock is not implemented/.test(e.message)) {
// register a manual vi.mock factory and import normally instead
}
throw e
} Prevention
- Use vi.importMock only in Node-environment tests.
- In browser tests, register mocks with vi.mock + factory and import the module normally.
- Tag browser-incompatible mocking helpers so they fail fast with a clear message.
When it happens
Trigger: Calling `vi.importMock('mod')` in a browser/bare-mocker environment.
Common situations: Browser tests calling `vi.importMock`; a non-Node worker pool; an environment misconfiguration.
Related errors
- importActual is not implemented
- Not called in the browser
- Looks like you set "test.environment" to "browser". To enabl
- stackblitz environment does not support the ${resolved.brows
- Cannot find environment for ${testFilePath}
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/112fa10f4ae724c4.json.
Report an issue: GitHub.