vitest-dev/vitest · error · TypeError
Environment ${environment.name} doesn't provide "getVmContex
Error message
Environment ${environment.name} doesn't provide "getVmContext" method. It should return a context created by "vm.createContext" method. What it means
After calling environment.setupVM(...) in the VM pool (vm.ts:73-77), Vitest expects the returned object to expose a getVmContext() method that yields the vm.Context. If that method is missing, this TypeError is thrown. The environment implemented setupVM but returned an object of the wrong shape.
Source
Thrown at packages/vitest/src/runtime/workers/vm.ts:74
}
const vm = await traces.$(
'vitest.runtime.environment.setup',
{
attributes: {
'vitest.environment': environment.name,
'vitest.environment.vite_environment': environment.viteEnvironment || environment.name,
},
},
() => environment.setupVM!(ctx.environment.options || ctx.config.environmentOptions || {}),
)
state.durations.environment = performance.now() - beforeEnvironmentTime
process.env.VITEST_VM_POOL = '1'
if (!vm.getVmContext) {
throw new TypeError(
`Environment ${environment.name} doesn't provide "getVmContext" method. It should return a context created by "vm.createContext" method.`,
)
}
const context: Context | null = vm.getVmContext()
if (!isContext(context)) {
throw new TypeError(
`Environment ${environment.name} doesn't provide a valid context. It should be created by "vm.createContext" method.`,
)
}
provideWorkerState(context, state)
// this is unfortunately needed for our own dependencies
// we need to find a way to not rely on this by default
// because browser doesn't provide these globals
context.process = processView on GitHub (pinned to d568f8ce37)
Solutions
- Make setupVM return an object exposing getVmContext: () => vm.createContext(...).
- Follow the Environment type (getVmContext is part of the VMEnvironment contract) rather than ad-hoc shapes.
- Add a teardown() method too so the VM context can be cleaned up.
Example fix
// before: returns context directly
setupVM(options) {
return vm.createContext({})
}
// after: returns object with getVmContext
setupVM(options) {
const context = vm.createContext({})
return { getVmContext: () => context }
} Defensive patterns
Strategy: type-guard
Type guard
interface VmEnvironmentResult { getVmContext: () => unknown; teardown?: () => void }
function hasGetVmContext(value: unknown): value is VmEnvironmentResult {
return !!value && typeof (value as any).getVmContext === 'function'
} Prevention
- Implement setupVM to return { getVmContext, teardown } exactly per the Environment type.
- Unit-test the environment's setupVM return shape before plugging it into the vm pool.
- Return the same context object from getVmContext on every call.
When it happens
Trigger: A custom environment whose setupVM returns an object without a getVmContext function — e.g. returns the context directly, or returns { context } instead of { getVmContext: () => context }.
Common situations: Implementing setupVM for the first time and returning the bare context; following an outdated guide; copy-pasting a setup-style return into setupVM.
Related errors
- Environment "${ctx.environment.name}" is not a valid environ
- Environment ${environment.name} doesn't provide a valid cont
- Not called in the browser
- Vitest mocker was not initialized in this environment. vi.${
- Environment "${name}" is not a valid environment. Path "${pa
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/8a0708a659313d60.json.
Report an issue: GitHub.