vitest-dev/vitest · error · TypeError

Environment doesn't provide "getVmContext" method. It…

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() successfully, Vitest requires the returned object to expose getVmContext(). If the returned environment instance has no getVmContext method it throws a TypeError explaining the method is missing and that it should return a vm.createContext() result.

Solutions

  1. Make setupVM return an object with getVmContext(): return { getVmContext: () => ctx }.
  2. Upgrade/downgrade the environment package to a version whose setupVM return shape matches Vitest's expectation.
  3. If the environment only supports setup(), use a non-vm pool instead.
  4. Verify the return value of setupVM in a quick script before wiring it into Vitest.

Example fix

// before: custom env
export default { name: 'my', setupVM() { return vm.createContext({}) } }

// after
export default {
  name: 'my',
  setupVM() { const ctx = vm.createContext({}); return { getVmContext: () => ctx } }
}
Defensive patterns

Strategy: type-guard

Validate before calling

const instance = await environment.setupVM(options)
if (typeof instance.getVmContext !== 'function') {
  throw new Error('setupVM must return { getVmContext }')
}

Type guard

function hasGetVmContext(vm) {
  return vm != null && typeof vm.getVmContext === 'function'
}

Prevention

When it happens

Trigger: An environment's setupVM() returns an object that lacks a getVmContext function. This is a contract violation: setupVM must return { getVmContext: () => vm.Context }.

Common situations: A custom vm environment that returns a bare context from setupVM instead of wrapping it, or returns undefined/null. Also a version of an environment that changed its setupVM return shape.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/8a0708a659313d60. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/runtime/workers/vm.ts:76

  }

  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.`,
    )
  }

  // captured before vitest installs its own globals (worker state, console,
  // mocker, executor symbol): they reference the test file's module graph, so
  // the teardown strip must treat them as removable, not as pristine
  const initialContextKeys = captureContextKeys(context)

  provideWorkerState(context, state)

View on GitHub (pinned to 1fa9837ec2)