{"record":{"id":"6685068dd1de640f","repo":"vitest-dev/vitest","slug":"there-are-some-problems-in-resolving-the-mocks-api","errorCode":null,"errorMessage":"There are some problems in resolving the mocks API.\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'.\nTo fix this issue you can either:\n- import the mocks API directly from 'vitest'\n- enable the 'globals' option","messagePattern":"There are some problems in resolving the mocks API\\.\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'\\.\nTo fix this issue you can either:\n- import the mocks API directly from 'vitest'\n- enable the 'globals' option","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/hoistMocks.ts","lineNumber":61,"sourceCode":"  codeFrameGenerator?: CodeFrameGenerator\n  magicString?: () => MagicString\n  /**\n   * Root of the project\n   * @default process.cwd()\n   */\n  root?: string\n  getMap?: () => Rollup.SourceMap\n}\n\nconst API_NOT_FOUND_ERROR = `There are some problems in resolving the mocks API.\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'.\nTo fix this issue you can either:\n- import the mocks API directly from 'vitest'\n- enable the 'globals' option`\n\nfunction API_NOT_FOUND_CHECK(names: string[]) {\n  return `\\nif (${names.map(name => `typeof globalThis[\"${name}\"] === \"undefined\"`).join(' && ')}) `\n    + `{ throw new Error(${JSON.stringify(API_NOT_FOUND_ERROR)}) }\\n`\n}\n\nfunction isIdentifier(node: any): node is Positioned<Identifier> {\n  return node.type === 'Identifier'\n}\n\nfunction getNodeTail(code: string, node: Node) {\n  let end = node.end\n  if (code[node.end] === ';') {\n    end += 1\n  }\n  if (code[node.end] === '\\n') {\n    return end + 1\n  }\n  if (code[node.end + 1] === '\\n') {\n    end += 1\n  }\n  return end","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/node/hoistMocks.ts#L43-L79","documentation":"This is a runtime error injected into transformed test code by `hoistMocks` via `API_NOT_FOUND_CHECK`. After hoisting, `vi.mock`/`vi.hoisted` calls are rewritten to reference `globalThis[\"vi\"]` (or the configured accessor). The injected check verifies those globals exist at runtime; if not, it throws this guidance message. It means the Vitest mocking API (`vi`) was not installed on `globalThis` when the hoisted code executed.","triggerScenarios":"Importing `vi`/`vitest` from a module other than `vitest` (so the hoister does not recognize it as the hoisted module), or running transformed test code outside the Vitest runtime that would normally set `globalThis.vi`. Also triggered when `globals: false` and the test imports `vi` from a re-exporting wrapper that the hoister does not whitelist.","commonSituations":"Re-exporting `vi` from a shared test-helpers module (`export { vi } from 'vitest'`) and importing it back; running Vitest-transformed files under a different runner (plain Node, Jest); `globals: false` combined with an indirect import path; tree-shaking or instrumentation stripping the globalThis setup.","solutions":["Enable `globals: true` in the Vitest config so `vi` is installed on `globalThis`.","Import the mocking API directly from `vitest` (`import { vi } from 'vitest'`) rather than from a re-exporting helper.","If re-exporting is required, add the specifier to the hoister's recognized modules (Vitest recognizes `vite-plus/test` by default; custom forks need corresponding config).","Run the file under `vitest`, not plain Node or another test runner."],"exampleFix":"// before — re-exported import breaks hoisting\n// helpers.ts: export { vi } from 'vitest'\nimport { vi } from './helpers'\nvi.mock('./mod')\n// after — import directly from vitest\nimport { vi } from 'vitest'\nvi.mock('./mod')","handlingStrategy":"validation","validationCode":"// Confirm the mocking API is installed on globalThis before tests run.\nif (typeof (globalThis as any).vi === 'undefined') {\n  throw new Error('Vitest mocking API not installed. Set globals: true or import vi from \"vitest\".')\n}","typeGuard":"function isVitestApiInstalled(name: string): boolean {\n  return typeof (globalThis as any)[name] !== 'undefined'\n}","tryCatchPattern":null,"preventionTips":["Enable `test.globals: true` in the Vitest config.","Import vi directly from 'vitest', not from a re-exporting helper.","Run transformed test files under the Vitest runtime only."],"tags":["hoist-mocks","globals","vi-mock","runtime-injected"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}