vitest-dev/vitest · error · TypeError

Environment ${environment.name} doesn't provide a valid cont

Error message

Environment ${environment.name} doesn't provide a valid context. It should be created by "vm.createContext" method.

What it means

Even when getVmContext exists, the VM pool validates its return value with vm.isContext (vm.ts:81-85). If getVmContext returns null, a plain object, or anything not created by vm.createContext, this TypeError fires. The environment's contract is to return a genuine node:vm context.

Source

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

      },
    },
    () => 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 = process
  context.global = context
  context.console = state.config.disableConsoleIntercept
    ? console
    : createCustomConsole(state)
  // TODO: don't hardcode setImmediate in fake timers defaults
  context.setImmediate = setImmediate
  context.clearImmediate = clearImmediate

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Ensure getVmContext always returns the exact value returned by vm.createContext(...).
  2. Create the context once in setupVM and cache it so getVmContext cannot fail on repeated calls.
  3. Guard against null before returning (throw a clearer error inside the environment).

Example fix

// before: returns a non-context
getVmContext() {
  return this.options.context ?? globalThis
}

// after: always a real vm context
import vm from 'node:vm'
setupVM() {
  this.context = vm.createContext({})
  return this
}
getVmContext() {
  return this.context
}
Defensive patterns

Strategy: validation

Validate before calling

import vm from 'node:vm'

function assertValidVmContext(ctx: unknown) {
  if (!vm.isContext(ctx)) {
    throw new TypeError(
      'getVmContext must return a value created by vm.createContext'
    )
  }
}

Type guard

import vm from 'node:vm'

function isVmContext(value: unknown): value is vm.Context {
  return vm.isContext(value)
}

Prevention

When it happens

Trigger: A custom environment whose getVmContext returns null, undefined, a global object, a Proxy, or any value that was not produced by vm.createContext(...). Reached right after `const context = vm.getVmContext()` when isContext(context) is false.

Common situations: getVmContext lazily creating the context but failing silently and returning undefined; returning globalThis thinking it is a context; returning a plain object that mimics a context.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/1da80606d974cb0f.json. Report an issue: GitHub.