{"id":"c13be8a03b456ed6","repo":"jestjs/jest","slug":"you-have-requested-globalvariable-as-a-global","errorCode":null,"errorMessage":"You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.","messagePattern":"You have requested '(.+?)' as a global variable, but it was not present\\. Please check your config or your global environment\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/ModuleExecutor.ts","lineNumber":145,"sourceCode":"\n      const transformedCode = this.transformCache.transform(filename, options);\n\n      const compiledFunction = this.compile(transformedCode, filename);\n      if (compiledFunction === null) {\n        return 'env-disposed';\n      }\n\n      const jestObject = this.jestGlobals.jestObjectFor(filename);\n\n      const lastArgs: [Jest | undefined, ...Array<Global.Global>] = [\n        this.config.injectGlobals ? jestObject : undefined,\n        ...this.config.sandboxInjectedGlobals.map<Global.Global>(\n          globalVariable => {\n            if (this.environment.global[globalVariable]) {\n              return this.environment.global[globalVariable];\n            }\n\n            throw new Error(\n              `You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,\n            );\n          },\n        ),\n      ];\n\n      if (!this.testMainModule.current && filename === this.testPath) {\n        this.testMainModule.current = module;\n      }\n\n      Object.defineProperty(module, 'main', {\n        enumerable: true,\n        value: this.testMainModule.current,\n      });\n\n      try {\n        compiledFunction.call(\n          module.exports,","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/ModuleExecutor.ts#L127-L163","documentation":"Thrown by ModuleExecutor when compiling a module whose sandboxed global environment is missing one of the names listed in `config.sandboxInjectedGlobals`. The runtime iterates each requested global name (ModuleExecutor.ts:139-148) and reads it from `this.environment.global`; if that property is falsy it throws. It indicates a mismatch between Jest config and the test environment's setup.","triggerScenarios":"Config sets `sandboxInjectedGlobals: ['MyGlobal']` (or `injectGlobals`-adjacent plumbing feeds a name), but the configured `testEnvironment` never assigned `global.MyGlobal`. Common when `jest-environment-node` is used but the global was only set in a custom environment's `setup()`, or when the name is misspelled between config and environment.","commonSituations":"Switching from a custom environment to `node`/`jsdom` environment and forgetting to migrate the global assignment. Renaming a global in config without updating the environment script. Using `setupFiles` (which runs before the environment is fully ready) instead of `setupFilesAfterEnv` or environment `setup()`.","solutions":["Verify the global is actually assigned inside the environment: in a custom environment class set `this.global.MyGlobal = ...` in `setup()`, or in a setup file set `globalThis.MyGlobal = ...`.","Check spelling/casing of every entry in `sandboxInjectedGlobals` against what the environment writes.","If the global should be supplied by user code, move the assignment from `setupFiles` to `setupFilesAfterEnv` or a custom environment's `setup()`.","Remove the entry from `sandboxInjectedGlobals` if it's no longer needed."],"exampleFix":"// before (jest.config.js)\nmodule.exports = { sandboxInjectedGlobals: ['MyGlobal'] };\n// global never set anywhere\n\n// after — assign it in a custom environment or setup file\nglobalThis.MyGlobal = require('./myGlobal');","handlingStrategy":"validation","validationCode":"const Config = require('./jest.config');\nconst required = Config.sandboxInjectedGlobals ?? [];\nconst missing = required.filter(name => !globalThis[name]);\nif (missing.length > 0) {\n  throw new Error(`Missing globals: ${missing.join(', ')}`);\n}","typeGuard":"function hasAllGlobals(env: typeof globalThis, names: string[]): boolean {\n  return names.every(n => Boolean(env[n as keyof typeof env]));\n}","tryCatchPattern":null,"preventionTips":["Keep one source of truth for global names and reuse it in both the config and the environment setup.","Run a tiny smoke test in CI that loads the config and asserts each global is defined before running the suite.","Use a custom environment's setup() to define globals rather than ad-hoc setup files so the contract is explicit."],"tags":["config","environment","globals","sandbox"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}