jestjs/jest · critical · Error

JSDOM did not return a Window object

Error message

JSDOM did not return a Window object

What it means

Thrown after `new JSDOM(...)` when `this.dom.window` is null/undefined. JSDOM is expected to return a Window object; if it does not, the environment cannot proceed. This is a defensive check (`global == null`) after JSDOM construction.

Solutions

  1. Reinstall `jsdom` and `jest-environment-jsdom` to rule out a corrupted install.
  2. Check that no test setup or mock is overriding the `jsdom` module.
  3. Verify Node.js version meets the minimum requirement for your jsdom version.
  4. Try a minimal `new (require('jsdom').JSDOM)('<html></html>')` in a standalone script to isolate.

Example fix

// diagnosis script
const { JSDOM } = require('jsdom');
const dom = new JSDOM('<!DOCTYPE html>');
console.log(typeof dom.window); // should print 'object'
Defensive patterns

Strategy: validation

Validate before calling

// Smoke-test JSDOM window creation
function smokeTestJsdom() {
  const { JSDOM } = require('jsdom');
  const dom = new JSDOM('<!DOCTYPE html>');
  if (dom.window == null) {
    throw new Error('JSDOM failed to produce a window — check jsdom installation');
  }
}

Type guard

function hasJsdomWindow(dom: unknown): dom is { window: object } {
  return dom != null && typeof (dom as any).window === 'object' && (dom as any).window !== null;
}

Prevention

When it happens

Trigger: JSDOM constructor returns an object whose `.window` is null — this can happen with a broken JSDOM build, a jsdom constructor called with invalid options that silently produces no window, or a jsdom module that is a mock/stub.

Common situations: A test setup that mocks or stubs `jsdom` globally. A corrupted or partial jsdom installation. Using an extremely old jsdom version that has different constructor behavior. Memory exhaustion causing JSDOM to fail silently.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/46eeb94265b2c432. Report an issue: GitHub.

Appendix: source

Thrown at packages/jest-environment-jsdom-abstract/src/index.ts:98

        : '<!DOCTYPE html>',
      {
        pretendToBeVisual: true,
        resources:
          typeof projectConfig.testEnvironmentOptions.userAgent === 'string'
            ? new ResourceLoader({
                userAgent: projectConfig.testEnvironmentOptions.userAgent,
              })
            : undefined,
        runScripts: 'dangerously',
        url: 'http://localhost/',
        virtualConsole,
        ...projectConfig.testEnvironmentOptions,
      },
    );
    const global = (this.global = this.dom.window as unknown as Win);

    if (global == null) {
      throw new Error('JSDOM did not return a Window object');
    }

    // TODO: remove at some point - for "universal" code (code should use `globalThis`)
    global.global = global;

    // Node's error-message stack size is limited at 10, but it's pretty useful
    // to see more than that when a test fails.
    this.global.Error.stackTraceLimit = 100;
    installCommonGlobals(global, projectConfig.globals);

    // TODO: remove this ASAP, but it currently causes tests to run really slow
    global.Buffer = Buffer;

    // Report uncaught errors.
    this.errorEventListener = event => {
      if (userErrorListenerCount === 0 && event.error != null) {
        process.emit('uncaughtException', event.error);
      }

View on GitHub (pinned to 8e6d128e4a)