match-media-mock provides a server-side compatible substitution for the browser's `Window.matchMedia()` API. It allows developers to simulate media queries in non-browser environments, such as Node.js, making it useful for server-side rendering (SSR) of applications that rely on responsive styles or conditional rendering based on media features. The package, currently at version 0.1.1, appears to be unmaintained with its last significant update approximately 8 years ago. Its core functionality is built upon the `css-mediaquery` library for parsing and evaluating media query strings. It was specifically designed for integration with styling libraries like Radium to enable isomorphic React applications to correctly apply styles based on simulated viewport dimensions during server rendering. Due to its age, it lacks modern features and might not be compatible with contemporary module systems (ESM) or testing frameworks without additional shims, and more active alternatives now exist.
npm install match-media-mockVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create and configure a `match-media-mock` instance, evaluate media queries against the configured environment, and manage event listeners for media query changes.
Consider migrating to actively maintained alternatives like `mock-match-media` or `jest-matchmedia-mock` for testing environments, or implement a custom polyfill/mock for production SSR needs.
Always use `const matchMediaMock = require('match-media-mock').create();` to import and instantiate the mock. If used in an ESM project, consider using a CommonJS wrapper or a bundler that correctly handles CJS modules.Always manually invoke `mediaQueryList.callListeners()` on any `MediaQueryList` objects you wish to update after calling `matchMediaMock.setConfig()`.
Ensure you call `create()` to get the executable function: `const matchMediaMock = require('match-media-mock').create();`Ensure the mock is initialized and configured *before* any code that attempts to access `window.matchMedia`. If you intend to patch `window`, you might need to manually assign the mock to `window.matchMedia` or use a different library designed for global polyfilling.