{"record":{"id":"4b7f8fadbd3a4b4d","repo":"testing-library/react-testing-library","slug":"unable-to-locate-global-object","errorCode":null,"errorMessage":"unable to locate global object","messagePattern":"unable to locate global object","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/act-compat.js","lineNumber":25,"sourceCode":"function getGlobalThis() {\n  /* istanbul ignore else */\n  if (typeof globalThis !== 'undefined') {\n    return globalThis\n  }\n  /* istanbul ignore next */\n  if (typeof self !== 'undefined') {\n    return self\n  }\n  /* istanbul ignore next */\n  if (typeof window !== 'undefined') {\n    return window\n  }\n  /* istanbul ignore next */\n  if (typeof global !== 'undefined') {\n    return global\n  }\n  /* istanbul ignore next */\n  throw new Error('unable to locate global object')\n}\n\nfunction setIsReactActEnvironment(isReactActEnvironment) {\n  getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment\n}\n\nfunction getIsReactActEnvironment() {\n  return getGlobalThis().IS_REACT_ACT_ENVIRONMENT\n}\n\nfunction withGlobalActEnvironment(actImplementation) {\n  return callback => {\n    const previousActEnvironment = getIsReactActEnvironment()\n    setIsReactActEnvironment(true)\n    try {\n      // The return value of `act` is always a thenable.\n      let callbackNeedsToBeAwaited = false\n      const actResult = actImplementation(() => {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/testing-library/react-testing-library/blob/20ce75f2907ca0e5c5a8ae595c0e9a4e368c7800/src/act-compat.js#L7-L43","documentation":"getGlobalThis() in src/act-compat.js resolves the JavaScript global object by probing globalThis, then self, window, and finally global. It throws 'unable to locate global object' only when none of these identifiers are defined, which means the code is running in a non-standard sandbox that exposes no global object at all. The library needs the global to set/read IS_REACT_ACT_ENVIRONMENT for act() support.","triggerScenarios":"Calling render, rerender, or unmount (which call setIsReactActEnvironment/getIsReactActEnvironment) in an environment where typeof globalThis, self, window, and global are all 'undefined' — e.g. an exotic VM sandbox, a restricted eval realm, or a custom Node vm context with no globals attached.","commonSituations":"Running tests inside an isolated vm2/Node vm sandbox, embedding the library in a worker-like realm stripped of globals, misconfigured bundler environments, or SSR/tooling contexts with no DOM and no Node global polyfill.","solutions":["Run tests in a standard environment (jsdom/happy-dom testEnvironment in Jest or Vitest) so globalThis/window exist.","If using a Node vm sandbox, attach a global object to the context (e.g. context.globalThis = context) before importing the library.","Upgrade Node.js or the runtime — modern engines always define globalThis (ES2020+).","Check bundler/build config that the entry is not evaluated in a realm with deleted globals (no `globalThis = undefined` shenanigans)."],"exampleFix":"// vitest.config.js — before\ntest: { environment: 'node' }\n// after\ntest: { environment: 'jsdom' }","handlingStrategy":"validation","validationCode":"if (typeof globalThis === 'undefined' && typeof self === 'undefined' && typeof window === 'undefined' && typeof global === 'undefined') {\n  throw new Error('No global object available; @testing-library/react needs a DOM/Node global. Configure jsdom test environment.')\n}","typeGuard":"function hasGlobalObject() {\n  return typeof globalThis !== 'undefined' || typeof self !== 'undefined' || typeof window !== 'undefined' || typeof global !== 'undefined'\n}","tryCatchPattern":"try {\n  render(<App />)\n} catch (e) {\n  if (e.message === 'unable to locate global object') {\n    throw new Error('Test environment has no global object; use jsdom/happy-dom test environment or a standard Node context.')\n  }\n  throw e\n}","preventionTips":["Always run RTL tests with a DOM test environment (jsdom or happy-dom).","Avoid evaluating test code inside bare Node vm contexts without attached globals.","Target runtimes that support ES2020 globalThis.","Smoke-test your test setup with a trivial render test in CI before scaling out."],"tags":["environment","globals","test-setup","runtime"],"backgroundTag":"missing-global-object","analyzedSha":"20ce75f2907ca0e5c5a8ae595c0e9a4e368c7800","analyzedAt":"2026-09-02T16:11:03.216Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}