vitest-dev/vitest · error · Error
Don't have access to the "vitest" instance yet. This is a bu
Error message
Don't have access to the "vitest" instance yet. This is a bug in Vitest.
What it means
`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.
Source
Thrown at packages/vitest/src/node/config/pluginHarness.ts:29
/**
* @internal
*/
public _browserLastPort = defaultBrowserPort
constructor(
public logger: Logger = new Logger(),
public packageInstaller: VitestPackageInstaller = new VitestPackageInstaller(),
) {}
setVitest(vitest: Vitest | undefined): this {
this.vitest = vitest
return this
}
getVitest(): Vitest {
if (!this.vitest) {
throw new Error(`Don't have access to the "vitest" instance yet. This is a bug in Vitest.`)
}
return this.vitest
}
}
View on GitHub (pinned to 1fa9837ec2)
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.
Example fix
// before — reads harness before core is set
const vitest = harness.getVitest()
// after — defer to a later hook
export default {
name: 'my-plugin',
configureServer(server) {
const vitest = harness.getVitest() // core is wired up by now
},
} Defensive patterns
Strategy: type-guard
Validate before calling
// Defer reads until the harness is wired up.
function getVitestOrNull(harness: { vitest?: unknown }) {
return harness.vitest
}
const v = getVitestOrNull(harness)
if (!v) {
// skip or queue work instead of calling getVitest()
return
} Type guard
function hasVitest(harness: { vitest?: unknown }): harness is { vitest: NonNullable<typeof harness.vitest> } {
return harness.vitest != null
} Prevention
- 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.
When it happens
Trigger: 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`.
Common situations: 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.
Related errors
- Data was not properly initialized. This is a bug in Vitest.
- Worker state is not found. This is an issue with Vitest. Ple
- stopChunkTrace cannot be called outside of the test file.
- This command can only be called inside a test file.
- provider ${context.provider.name} is not supported
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/2607e3a6a7d5289d.
Report an issue: GitHub.