{"id":"1da80606d974cb0f","repo":"vitest-dev/vitest","slug":"environment-environment-name-doesn-t-provide-a","errorCode":null,"errorMessage":"Environment ${environment.name} doesn't provide a valid context. It should be created by \"vm.createContext\" method.","messagePattern":"Environment (.+?) doesn't provide a valid context\\. It should be created by \"vm\\.createContext\" method\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/workers/vm.ts","lineNumber":82,"sourceCode":"      },\n    },\n    () => environment.setupVM!(ctx.environment.options || ctx.config.environmentOptions || {}),\n  )\n\n  state.durations.environment = performance.now() - beforeEnvironmentTime\n\n  process.env.VITEST_VM_POOL = '1'\n\n  if (!vm.getVmContext) {\n    throw new TypeError(\n      `Environment ${environment.name} doesn't provide \"getVmContext\" method. It should return a context created by \"vm.createContext\" method.`,\n    )\n  }\n\n  const context: Context | null = vm.getVmContext()\n\n  if (!isContext(context)) {\n    throw new TypeError(\n      `Environment ${environment.name} doesn't provide a valid context. It should be created by \"vm.createContext\" method.`,\n    )\n  }\n\n  provideWorkerState(context, state)\n\n  // this is unfortunately needed for our own dependencies\n  // we need to find a way to not rely on this by default\n  // because browser doesn't provide these globals\n  context.process = process\n  context.global = context\n  context.console = state.config.disableConsoleIntercept\n    ? console\n    : createCustomConsole(state)\n  // TODO: don't hardcode setImmediate in fake timers defaults\n  context.setImmediate = setImmediate\n  context.clearImmediate = clearImmediate\n","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/workers/vm.ts#L64-L100","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure getVmContext always returns the exact value returned by vm.createContext(...).","Create the context once in setupVM and cache it so getVmContext cannot fail on repeated calls.","Guard against null before returning (throw a clearer error inside the environment)."],"exampleFix":"// before: returns a non-context\ngetVmContext() {\n  return this.options.context ?? globalThis\n}\n\n// after: always a real vm context\nimport vm from 'node:vm'\nsetupVM() {\n  this.context = vm.createContext({})\n  return this\n}\ngetVmContext() {\n  return this.context\n}","handlingStrategy":"validation","validationCode":"import vm from 'node:vm'\n\nfunction assertValidVmContext(ctx: unknown) {\n  if (!vm.isContext(ctx)) {\n    throw new TypeError(\n      'getVmContext must return a value created by vm.createContext'\n    )\n  }\n}","typeGuard":"import vm from 'node:vm'\n\nfunction isVmContext(value: unknown): value is vm.Context {\n  return vm.isContext(value)\n}","tryCatchPattern":null,"preventionTips":["Create the context once with vm.createContext and store it; return that stored reference from getVmContext.","Never return globalThis or a plain object from getVmContext.","Add an internal assertion inside the environment that throws a clear error if the context is null."],"tags":["vm-pool","environment","vm-context","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}