{"record":{"id":"2607e3a6a7d5289d","repo":"vitest-dev/vitest","slug":"don-t-have-access-to-the-vitest-instance-yet-th","errorCode":null,"errorMessage":"Don't have access to the \"vitest\" instance yet. This is a bug in Vitest.","messagePattern":"Don't have access to the \"vitest\" instance yet\\. This is a bug in Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/config/pluginHarness.ts","lineNumber":29,"sourceCode":"\n  /**\n   * @internal\n   */\n  public _browserLastPort = defaultBrowserPort\n\n  constructor(\n    public logger: Logger = new Logger(),\n    public packageInstaller: VitestPackageInstaller = new VitestPackageInstaller(),\n  ) {}\n\n  setVitest(vitest: Vitest | undefined): this {\n    this.vitest = vitest\n    return this\n  }\n\n  getVitest(): Vitest {\n    if (!this.vitest) {\n      throw new Error(`Don't have access to the \"vitest\" instance yet. This is a bug in Vitest.`)\n    }\n    return this.vitest\n  }\n}\n","sourceCodeStart":11,"sourceCodeEnd":34,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/config/pluginHarness.ts#L11-L34","documentation":"`PluginHarness.getVitest()` returns the stored `Vitest` instance but throws if `setVitest` was never called (or was called with `undefined`). This harness is an internal seam used to share the Vitest instance with plugins; hitting the error means a code path reached the harness before the core was wired up, which the message itself flags as a Vitest bug rather than user error.","triggerScenarios":"A plugin or internal module calls `harness.getVitest()` during construction or config resolution — before `setVitest(server)` runs in the Vitest bootstrap. Also reproducible in tests that instantiate `PluginHarness` directly without calling `setVitest`.","commonSituations":"Custom plugin that reads the Vitest instance in a hook that fires very early; partial mocks in unit tests; a regression in bootstrap ordering after a Vitest upgrade.","solutions":["If this is your code calling the harness, defer the read until a lifecycle hook that runs after the core is ready (e.g. `configResolved`/`buildStart` rather than module top-level).","Call `harness.setVitest(vitest)` before invoking `getVitest()` in tests.","If you have no custom plugin and hit this, report it as a Vitest bug with the plugin list and stack trace."],"exampleFix":"// before — reads harness before core is set\nconst vitest = harness.getVitest()\n\n// after — defer to a later hook\nexport default {\n  name: 'my-plugin',\n  configureServer(server) {\n    const vitest = harness.getVitest() // core is wired up by now\n  },\n}","handlingStrategy":"type-guard","validationCode":"// Defer reads until the harness is wired up.\nfunction getVitestOrNull(harness: { vitest?: unknown }) {\n  return harness.vitest\n}\n\nconst v = getVitestOrNull(harness)\nif (!v) {\n  // skip or queue work instead of calling getVitest()\n  return\n}","typeGuard":"function hasVitest(harness: { vitest?: unknown }): harness is { vitest: NonNullable<typeof harness.vitest> } {\n  return harness.vitest != null\n}","tryCatchPattern":null,"preventionTips":["Read the harness only inside lifecycle hooks that fire after bootstrap.","Prefer optional access (`harness.vitest?.`) to fail-soft before bootstrap.","In tests, always call `setVitest(...)` before exercising the harness."],"tags":["internal","plugin-harness","bootstrap","vitest-instance"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}