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

  1. Move the test to the Node environment (`// @vitest-environment node`).
  2. Avoid `vi.importMock` in browser contexts.
  3. 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

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


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