{"id":"8a0708a659313d60","repo":"vitest-dev/vitest","slug":"environment-environment-name-doesn-t-provide-g","errorCode":null,"errorMessage":"Environment ${environment.name} doesn't provide \"getVmContext\" method. It should return a context created by \"vm.createContext\" method.","messagePattern":"Environment (.+?) doesn't provide \"getVmContext\" method\\. It should return a context created by \"vm\\.createContext\" method\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/workers/vm.ts","lineNumber":74,"sourceCode":"  }\n\n  const vm = await traces.$(\n    'vitest.runtime.environment.setup',\n    {\n      attributes: {\n        'vitest.environment': environment.name,\n        'vitest.environment.vite_environment': environment.viteEnvironment || environment.name,\n      },\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","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/workers/vm.ts#L56-L92","documentation":"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.","triggerScenarios":"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 }.","commonSituations":"Implementing setupVM for the first time and returning the bare context; following an outdated guide; copy-pasting a setup-style return into setupVM.","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."],"exampleFix":"// before: returns context directly\nsetupVM(options) {\n  return vm.createContext({})\n}\n\n// after: returns object with getVmContext\nsetupVM(options) {\n  const context = vm.createContext({})\n  return { getVmContext: () => context }\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"interface VmEnvironmentResult { getVmContext: () => unknown; teardown?: () => void }\n\nfunction hasGetVmContext(value: unknown): value is VmEnvironmentResult {\n  return !!value && typeof (value as any).getVmContext === 'function'\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["vm-pool","environment","getvmcontext","setupvm"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}