{"record":{"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":84,"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  // captured before vitest installs its own globals (worker state, console,\n  // mocker, executor symbol): they reference the test file's module graph, so\n  // the teardown strip must treat them as removable, not as pristine\n  const initialContextKeys = captureContextKeys(context)\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","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/workers/vm.ts#L66-L102","documentation":"Even when getVmContext exists, Vitest calls it and passes the result to vm.isContext(). If that returns false (the value was not created by vm.createContext), it throws a TypeError stating the environment did not provide a valid context. This guards against environments returning a plain object, null, or a reused/disposed context.","triggerScenarios":"environment.getVmContext() returns something that is not a real vm context — e.g. a plain object, a Proxy, null, or a context that was already disposed. vm.isContext() is the canonical check for a context created via vm.createContext.","commonSituations":"A custom environment whose getVmContext returns a global object or a sandbox object that was never passed through vm.createContext; returning undefined accidentally; or a context disposed in a previous run being reused.","solutions":["Ensure getVmContext returns the exact value returned by vm.createContext() (store it, return it).","Do not dispose or recreate the context between setup and getVmContext.","Add a guard in the environment: if (!vm.isContext(ctx)) throw before returning.","Upgrade the environment package; this is a contract bug in the environment."],"exampleFix":"// before\ngetVmContext() { return this.sandbox /* plain object */ }\n\n// after\nconst ctx = vm.createContext(this.sandbox)\ngetVmContext() { return ctx }","handlingStrategy":"validation","validationCode":"import vm from 'node:vm'\nconst ctx = environment.getVmContext()\nif (!vm.isContext(ctx)) throw new Error('not a vm context')","typeGuard":"function isValidVmContext(ctx) {\n  return require('node:vm').isContext(ctx)\n}","tryCatchPattern":null,"preventionTips":["Always create the context with vm.createContext and return that exact value.","Never reuse a disposed context across runs.","Guard inside getVmContext with vm.isContext before returning."],"tags":["environment","vm","context","iscontext"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}