{"id":"112fa10f4ae724c4","repo":"vitest-dev/vitest","slug":"importmock-is-not-implemented","errorCode":null,"errorMessage":"importMock is not implemented","messagePattern":"importMock is not implemented","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/moduleRunner/bareModuleMocker.ts","lineNumber":292,"sourceCode":"      const redirect = this.findMockRedirect(id, external)\n      if (redirect) {\n        registry.register('redirect', originalId, id, url, redirect)\n      }\n      else {\n        registry.register('automock', originalId, id, url)\n      }\n    }\n\n    // every time the mock is registered, we remove the previous one from the cache\n    this.invalidateModuleById(id)\n  }\n\n  async importActual<T>(_rawId: string, _importer: string, _callstack?: string[] | null): Promise<T> {\n    throw new Error(`importActual is not implemented`)\n  }\n\n  async importMock<T>(_rawId: string, _importer: string, _callstack?: string[] | null): Promise<T> {\n    throw new Error(`importMock is not implemented`)\n  }\n\n  public queueMock(\n    id: string,\n    importer: string,\n    factoryOrOptions?: MockFactory | MockOptions,\n  ): void {\n    const mockType = getMockType(factoryOrOptions)\n    BareModuleMocker.pendingIds.push({\n      action: 'mock',\n      id,\n      importer,\n      factory: typeof factoryOrOptions === 'function' ? factoryOrOptions : undefined,\n      type: mockType,\n    })\n  }\n\n  public queueUnmock(id: string, importer: string): void {","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/moduleRunner/bareModuleMocker.ts#L274-L310","documentation":"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.","triggerScenarios":"Calling `vi.importMock('mod')` in a browser/bare-mocker environment.","commonSituations":"Browser tests calling `vi.importMock`; a non-Node worker pool; an environment misconfiguration.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const isNode = typeof process !== 'undefined' && process.versions?.node\nif (!isNode) {\n  throw new Error('vi.importMock is unavailable in this environment')\n}\nconst mocked = await vi.importMock('mod')","typeGuard":null,"tryCatchPattern":"try {\n  return await vi.importMock('mod')\n} catch (e) {\n  if (e instanceof Error && /importMock is not implemented/.test(e.message)) {\n    // register a manual vi.mock factory and import normally instead\n  }\n  throw e\n}","preventionTips":["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."],"tags":["mocking","import-mock","environment","browser","not-implemented"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}