Vitest / JSDOM TypeError: `window.matchMedia` is not a function

JSDOM does not implement browser CSS media-query APIs by default.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Vitest / JSDOM TypeError Platform: JavaScript
Common Root Causes:
  • JSDOM does not implement browser CSS media-query APIs by default.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Stub `window.matchMedia` in Vitest `setupFiles` or run component tests in real Chromium.
Safe Diagnostic Command:
Check test setupFiles configuration
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

JSDOM does not implement browser CSS media-query APIs by default. Recommended diagnostic: `Check test setupFiles configuration`. Remediation: Stub `window.matchMedia` in Vitest `setupFiles` or run component tests in real Chromium.

Worked Examples

  • Run `Check test setupFiles configuration` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-vitest-jsdom-matchmedia-not-function · Independently verified: 2026-11-25

Related Resources