testing-library/react-testing-library · error · Error

unable to locate global object

Error message

unable to locate global object

What it means

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.

Source

Thrown at src/act-compat.js:25

function getGlobalThis() {
  /* istanbul ignore else */
  if (typeof globalThis !== 'undefined') {
    return globalThis
  }
  /* istanbul ignore next */
  if (typeof self !== 'undefined') {
    return self
  }
  /* istanbul ignore next */
  if (typeof window !== 'undefined') {
    return window
  }
  /* istanbul ignore next */
  if (typeof global !== 'undefined') {
    return global
  }
  /* istanbul ignore next */
  throw new Error('unable to locate global object')
}

function setIsReactActEnvironment(isReactActEnvironment) {
  getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment
}

function getIsReactActEnvironment() {
  return getGlobalThis().IS_REACT_ACT_ENVIRONMENT
}

function withGlobalActEnvironment(actImplementation) {
  return callback => {
    const previousActEnvironment = getIsReactActEnvironment()
    setIsReactActEnvironment(true)
    try {
      // The return value of `act` is always a thenable.
      let callbackNeedsToBeAwaited = false
      const actResult = actImplementation(() => {

View on GitHub (pinned to 20ce75f290)

Solutions

  1. Run tests in a standard environment (jsdom/happy-dom testEnvironment in Jest or Vitest) so globalThis/window exist.
  2. If using a Node vm sandbox, attach a global object to the context (e.g. context.globalThis = context) before importing the library.
  3. Upgrade Node.js or the runtime — modern engines always define globalThis (ES2020+).
  4. Check bundler/build config that the entry is not evaluated in a realm with deleted globals (no `globalThis = undefined` shenanigans).

Example fix

// vitest.config.js — before
test: { environment: 'node' }
// after
test: { environment: 'jsdom' }
Defensive patterns

Strategy: validation

Validate before calling

if (typeof globalThis === 'undefined' && typeof self === 'undefined' && typeof window === 'undefined' && typeof global === 'undefined') {
  throw new Error('No global object available; @testing-library/react needs a DOM/Node global. Configure jsdom test environment.')
}

Type guard

function hasGlobalObject() {
  return typeof globalThis !== 'undefined' || typeof self !== 'undefined' || typeof window !== 'undefined' || typeof global !== 'undefined'
}

Try / catch

try {
  render(<App />)
} catch (e) {
  if (e.message === 'unable to locate global object') {
    throw new Error('Test environment has no global object; use jsdom/happy-dom test environment or a standard Node context.')
  }
  throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.


AI-assisted analysis of testing-library/react-testing-library@20ce75f290 (2026-09-02). Data as JSON: /api/errors/4b7f8fadbd3a4b4d. Report an issue: GitHub.