jestjs/jest · error · Error
You have requested '${globalVariable}' as a global variable,
Error message
You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment. What it means
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.
Source
Thrown at packages/jest-runtime/src/internals/ModuleExecutor.ts:145
const transformedCode = this.transformCache.transform(filename, options);
const compiledFunction = this.compile(transformedCode, filename);
if (compiledFunction === null) {
return 'env-disposed';
}
const jestObject = this.jestGlobals.jestObjectFor(filename);
const lastArgs: [Jest | undefined, ...Array<Global.Global>] = [
this.config.injectGlobals ? jestObject : undefined,
...this.config.sandboxInjectedGlobals.map<Global.Global>(
globalVariable => {
if (this.environment.global[globalVariable]) {
return this.environment.global[globalVariable];
}
throw new Error(
`You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,
);
},
),
];
if (!this.testMainModule.current && filename === this.testPath) {
this.testMainModule.current = module;
}
Object.defineProperty(module, 'main', {
enumerable: true,
value: this.testMainModule.current,
});
try {
compiledFunction.call(
module.exports,View on GitHub (pinned to f49721c78e)
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.
Example fix
// before (jest.config.js)
module.exports = { sandboxInjectedGlobals: ['MyGlobal'] };
// global never set anywhere
// after — assign it in a custom environment or setup file
globalThis.MyGlobal = require('./myGlobal'); Defensive patterns
Strategy: validation
Validate before calling
const Config = require('./jest.config');
const required = Config.sandboxInjectedGlobals ?? [];
const missing = required.filter(name => !globalThis[name]);
if (missing.length > 0) {
throw new Error(`Missing globals: ${missing.join(', ')}`);
} Type guard
function hasAllGlobals(env: typeof globalThis, names: string[]): boolean {
return names.every(n => Boolean(env[n as keyof typeof env]));
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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()`.
Related errors
- The --config option requires a JSON string literal, or a fil
- Cannot merge config in form of callback
- Whoops! Two projects resolved to the same config path: ${Str
- Jest: Cannot use configuration as an object without a file p
- There was an error while parsing the `--config` argument as
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/c13be8a03b456ed6.json.
Report an issue: GitHub.