{"record":{"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":76,"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  // 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)","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/workers/vm.ts#L58-L94","documentation":"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.","triggerScenarios":"An environment's setupVM() returns an object that lacks a getVmContext function. This is a contract violation: setupVM must return { getVmContext: () => vm.Context }.","commonSituations":"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.","solutions":["Make setupVM return an object with getVmContext(): return { getVmContext: () => ctx }.","Upgrade/downgrade the environment package to a version whose setupVM return shape matches Vitest's expectation.","If the environment only supports setup(), use a non-vm pool instead.","Verify the return value of setupVM in a quick script before wiring it into Vitest."],"exampleFix":"// before: custom env\nexport default { name: 'my', setupVM() { return vm.createContext({}) } }\n\n// after\nexport default {\n  name: 'my',\n  setupVM() { const ctx = vm.createContext({}); return { getVmContext: () => ctx } }\n}","handlingStrategy":"type-guard","validationCode":"const instance = await environment.setupVM(options)\nif (typeof instance.getVmContext !== 'function') {\n  throw new Error('setupVM must return { getVmContext }')\n}","typeGuard":"function hasGetVmContext(vm) {\n  return vm != null && typeof vm.getVmContext === 'function'\n}","tryCatchPattern":null,"preventionTips":["Implement getVmContext in any custom setupVM environment.","Return { getVmContext: () => ctx } from setupVM.","Pin environment versions whose setupVM shape matches Vitest."],"tags":["environment","vm","setupvm","getvmcontext"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}