{"id":"d1323431ca030307","repo":"vitest-dev/vitest","slug":"vitest-failed-to-find-message-one-of-the-follo","errorCode":null,"errorMessage":"Vitest failed to find ${message}. One of the following is possible:\n- \"vitest\" is imported directly without running \"vitest\" command\n- \"vitest\" is imported inside \"globalSetup\" (to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context)\n- \"vitest\" is imported inside Vite / Vitest config file\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\n","messagePattern":"Vitest failed to find (.+?)\\. One of the following is possible:\n- \"vitest\" is imported directly without running \"vitest\" command\n- \"vitest\" is imported inside \"globalSetup\" \\(to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context\\)\n- \"vitest\" is imported inside Vite / Vitest config file\n- Otherwise, it might be a Vitest bug\\. Please report it to https://github\\.com/vitest-dev/vitest/issues\n","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/vitest/src/runtime/runner/suite.ts","lineNumber":195,"sourceCode":" * });\n * ```\n * @example\n * ```ts\n * // Define a test with options\n * it('subtracts two numbers', { retry: 3 }, () => {\n *   expect(subtract(5, 2)).toBe(3);\n * });\n * ```\n */\nexport const it: TestAPI = test\n\nlet runner: VitestRunner\nlet defaultSuite: SuiteCollector\nlet currentTestFilepath: string\n\nfunction assert(condition: any, message: string) {\n  if (!condition) {\n    throw new Error(\n      `Vitest failed to find ${message}. One of the following is possible:`\n      + '\\n- \"vitest\" is imported directly without running \"vitest\" command'\n      + '\\n- \"vitest\" is imported inside \"globalSetup\" (to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context)'\n      + '\\n- \"vitest\" is imported inside Vite / Vitest config file'\n      + '\\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\\n',\n    )\n  }\n}\n\nexport function getDefaultSuite(): SuiteCollector<object> {\n  assert(defaultSuite, 'the default suite')\n  return defaultSuite\n}\n\nexport function getTestFilepath(): string {\n  return currentTestFilepath\n}\n","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/runner/suite.ts#L177-L213","documentation":"The internal `assert` helper (suite.ts:193-203) guards `getDefaultSuite`, `getCurrentSuite`, and `getRunner`. If the module-level `runner`/`defaultSuite`/`currentSuite` is unset, it means Vitest's runtime was not initialized — these globals are set by `clearCollectorContext` during normal test collection. This typically indicates `vitest` APIs (like `test`, `describe`, `getRunner`) were invoked outside the Vitest execution context.","triggerScenarios":"Importing `vitest` (or its internals) and calling `test`/`describe`/`getRunner` in a plain Node script, a `globalSetup` file, or the Vite/Vitest config file itself — anywhere Vitest hasn't run `clearCollectorContext`.","commonSituations":"Using Vitest globals in `globalSetup` (which runs in a different context); importing `vitest` directly to run tests manually; importing test APIs inside `vitest.config.ts`; SSR/build code accidentally importing test modules.","solutions":["Move code that uses `test`/`describe`/Vitest APIs from `globalSetup` into `setupFiles` (which run in the test context).","Do not import `vitest` inside the Vite/Vitest config file.","Ensure you invoke the Vitest CLI (`vitest`/`vitest run`) rather than executing test files directly with `node`.","If this appears during normal runs, report it as a Vitest bug with a reproduction."],"exampleFix":"// before: vitest.config.ts\nimport { describe, test } from 'vitest' // wrong context\n// after: remove test imports from config; put them in test files\n// globalSetup: don't use vitest APIs here\ndefault defineConfig({\n  test: { globalSetup: ['./setup-db.ts'] }, // setup-db.ts must NOT import vitest test APIs\n})","handlingStrategy":"validation","validationCode":"// Detect misuse: ensure vitest test APIs are not imported in config or globalSetup.\n// In CI, run a check that globalSetup files and vitest.config do not import from 'vitest' test APIs.\n// Programmatic check omitted; rely on not importing vitest in those contexts.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never import vitest test APIs (test/describe/expect) in vitest.config.ts or globalSetup files.","Use setupFiles (which run in test context) instead of globalSetup for test-scoped setup.","Always invoke tests via the vitest CLI, not `node`."],"tags":["initialization","config","globalsetup","context","internal"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}