Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest fails with window.matchMedia is not a function

ErrorTypeError: window.matchMedia is not a function

Short answer

jsdom does not implement window.matchMedia, so any component or library that calls it breaks under Jest. Define a mock with Object.defineProperty. Put it in the test file when the call happens at render time, or in a setup file listed in setupFilesAfterEnv when the module calls it as soon as it is imported.

  • Applies to Jest, jsdom test environment, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the top of the stack trace. If it points into your component's function body, the call runs at render time. If it points to module level code or a library import, it runs on import.

    npx jest path/to/Component.test.js
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace shows your component or a hook calling window.matchMediaThe call happens inside a function that the test runsFix
    2. 2The error is thrown while Jest loads the file, before any test starts, even though the mock is defined in the test bodyThe module calls matchMedia the moment it is importedFix

What does the error look like?

  • TypeError: window.matchMedia is not a function

Cause 1 of 2

The call happens inside a function that the test runs

1Check

The jest docs list matchMedia among methods not implemented in jsdom and give a mock for it.

grep -rn 'matchMedia' src/

2Fix

Define the mock in the test file before rendering.

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

3Verify

Run the test again.

npx jest path/to/Component.test.js

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 2

The module calls matchMedia the moment it is imported

1Check

Imports run in order, and a mock written below an import is defined too late.

head -5 path/to/Component.test.js

2Fix

Move the mock into its own file and import it first, or load it for all tests with setupFilesAfterEnv.

// matchMedia.mock.js holds the Object.defineProperty block above

// Component.test.js
import './matchMedia.mock'; // must come first
import { Component } from './Component';

// or, for every test file, in jest.config.js:
module.exports = {
  setupFilesAfterEnv: ['<rootDir>/matchMedia.mock.js'],
};

3Verify

Run the whole suite.

npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Keep jsdom mocks in one setup file so new tests inherit them.
  • Make the mock return matches: false for light layouts, and override it in tests that need a media query to match.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.