{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/hoistMocks.ts#L43-L79","documentation":"Thrown at packages/mocker/src/node/hoistMocks.ts:53-62 (the API_NOT_FOUND_ERROR string, injected as a runtime check by API_NOT_FOUND_CHECK). When hoistMocks detects vi/vitest usage but the symbols were imported from a module other than the hoisted module (vitest) and globals is disabled, it injects a runtime guard that throws if those names are undefined on globalThis. The message directs you to import the API from 'vitest' or enable globals.","triggerScenarios":"Using vi.* (e.g. vi.mock) after importing vi from a re-exporting package that is not 'vitest' (and not in REDISTRIBUTED_HOISTED_MODULES), with globals: false. The injected check at hoistMocks.ts:575-577 fires because the binding is not the real vitest global.","commonSituations":"Importing { vi } from a local wrapper module or third-party package instead of 'vitest'; a wrapper that re-exports vi but is not registered as a redistributed hoisted module; misconfigured globals option expecting vi to be a global while it was imported from elsewhere.","solutions":["Import the mocking API directly from 'vitest': import { vi } from 'vitest'.","Enable the globals option in vitest config (test.globals: true) so vi is available globally.","If redistributing Vitest's API under your own specifier, add it to REDISTRIBUTED_HOISTED_MODULES or configure hoistedModule accordingly."],"exampleFix":"// before\nimport { vi } from './test-utils' // re-exports vi\nvi.mock('./logger') // -> API_NOT_FOUND_ERROR\n\n// after\nimport { vi } from 'vitest'\nvi.mock('./logger')","handlingStrategy":"validation","validationCode":"// Ensure vi is imported from 'vitest' (or globals enabled) before relying on it.\nfunction assertMockApiAvailable() {\n  // When globals: false and vi is imported from a non-vitest source,\n  // the hoisted runtime check will throw. Validate at config time.\n  // In vitest.config.ts set test.globals = true, OR always import { vi } from 'vitest'.\n}","typeGuard":null,"tryCatchPattern":"try {\n  vi.mock('./logger')\n} catch (e) {\n  if (e instanceof Error && /resolving the mocks API/.test(e.message)) {\n    console.error('Import vi from \"vitest\" directly, or enable test.globals in vitest.config.ts')\n  }\n  throw e\n}","preventionTips":["Always import the mocking API directly from 'vitest': import { vi } from 'vitest'.","If you prefer not to import, set test.globals: true in vitest.config.ts.","Do not re-export vi from wrapper modules unless you also register them in REDISTRIBUTED_HOISTED_MODULES or configure hoistedModule."],"tags":["hoist-mocks","globals","import-source","mock-api"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}