{"record":{"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":"error","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 getRunner(): VitestRunner {\n  assert(runner, 'the runner')\n  return runner\n}","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/runner/suite.ts#L177-L213","documentation":"Thrown by the internal assert() helper when one of the module-level singletons (defaultSuite, runner, currentTestFilepath) is accessed before the Vitest runtime initialized them. The Vitest API (describe/test/expect) only works inside files executed by the Vitest runner process; importing these APIs in other contexts leaves the singletons unset.","triggerScenarios":"Importing 'vitest' (or its named exports test/describe/expect) from globalSetup, from the Vitest/Vite config file, from a plain Node script, or from a different process. Each of the four message variants points to one of these.","commonSituations":"Using test utilities inside globalSetup (which runs in a separate context); importing vitest in vitest.config.ts; running a test file directly with `node file.test.ts` instead of via the vitest CLI; importing vitest inside a Vite plugin.","solutions":["Move setup code that uses vitest APIs from globalSetup into setupFiles.","Do not import 'vitest' from config files; use plain Node APIs there.","Run tests via the vitest CLI (`vitest`, `vitest run`) rather than node/ts-node.","If the file is not a test, do not import vitest's test APIs at all."],"exampleFix":"// before: vitest.config.ts\nimport { describe } from 'vitest' // throws at load\n\n// after: vitest.config.ts\nimport { defineConfig } from 'vitest/config'\nexport default defineConfig({ test: { setupFiles: ['./setup.ts'] } })\n\n// setup.ts\nimport { expect } from 'vitest' // ok: runs in test context","handlingStrategy":"validation","validationCode":"// Ensure vitest APIs are only imported in files run by the vitest CLI.\n// In config files and globalSetup, use plain Node APIs.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not import vitest from vitest.config.ts or vite.config.ts.","Use setupFiles (not globalSetup) for code that needs vitest APIs.","Always run tests via the vitest CLI, never `node test.ts`.","Keep vitest imports out of library source that ships to non-test bundles."],"tags":["setup","config","import-context","runtime"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}