@deanwhillier/jest-matchmedia-mock

中文文档
1.2.0发布于 2021/12/03MITTypeScriptCommonJS零依赖近 30 天下载 2.9万

Mock for fully testing any media queries with Jest

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. API
  4. new MatchMediaMock()
  5. matchMedia.useMediaQuery()
  6. matchMedia.getMediaQueries()
  7. matchMedia.getListeners()
  8. matchMedia.clear()
  9. matchMedia.destroy()
  10. License

🃏 MatchMedia Mock for Jest

Travis (.org) Coveralls github npm GitHub top language npm type definitions node npm peer dependency version

This implementation of the window.matchMedia method allows you to control your media queries and their listening functions. You can update the media query that is currently applied to the document by simply calling one function.

Installation

NPM

npm i --save-dev jest-matchmedia-mock

Yarn

yarn add --dev jest-matchmedia-mock

Usage

import MatchMediaMock from 'jest-matchmedia-mock';

let matchMedia;

describe('Your testing module' => {
  beforeAll(() => {
    matchMedia = new MatchMediaMock();
  });

  afterEach(() => {
    matchMedia.clear();
  });

  test('Your test case', () => {
    const mediaQuery = '(prefers-color-scheme: light)';
    const firstListener = jest.fn();
    const secondListener = jest.fn();
    const mql = window.matchMedia(mediaQuery);

    mql.addEventListener("change", ev => ev.matches && firstListener());
    mql.addEventListener("change", ev => ev.matches && secondListener());

    matchMedia.useMediaQuery(mediaQuery);

    expect(firstListener).toBeCalledTimes(1);
    expect(secondListener).toBeCalledTimes(1);
  })
})

This works if window.matchMedia() is used in a function (or method) which is invoked in the test. If window.matchMedia() is executed directly in the tested file, Jest returns TypeError: window.matchMedia is not a function and doesn't properly execute the test.

In this case, the solution is to move the instantiation of the mock into a separate file and include this one in the test before the tested file:

import matchMedia from './matchMedia.mock.ts'; // Must be imported before the tested file
import { myMethod } from './file-to-test';

describe('myMethod()', () => {
  // Test the method here...
});

API

new MatchMediaMock()

Implements window.matchMedia and returns an instance with methods listed below

matchMedia.useMediaQuery()

Updates the currently used media query, and calls previously added listener functions registered for the passed media query

  • Arguments:

    • mediaQuery: string;
  • Returns: never | void

matchMedia.getMediaQueries()

Returns an array listing the media queries for which the matchMedia has registered listeners

  • Returns: string[]

matchMedia.getListeners()

Returns a copy of the array of listeners for the passed media query

  • Arguments:

    • mediaQuery: string;
  • Returns: MediaQueryListener[]

matchMedia.clear()

Clears all registered media queries and their listeners

matchMedia.destroy()

Clears all registered media queries and their listeners, and destroys the implementation of window.matchMedia

License

MIT

Copyright (c) 2020-present, Kirill Dyakov

版本与发布记录

@deanwhillier/jest-matchmedia-mock 共发布 1 个版本,首次发布于 2021/12/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2021/12/034 年前

关于 @deanwhillier/jest-matchmedia-mock 的常见问题

@deanwhillier/jest-matchmedia-mock 是什么?

Mock for fully testing any media queries with Jest

如何安装 @deanwhillier/jest-matchmedia-mock?

在项目目录运行 npm install @deanwhillier/jest-matchmedia-mock,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:jest(>=13)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@deanwhillier/jest-matchmedia-mock 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deanwhillier/jest-matchmedia-mock 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@deanwhillier/jest-matchmedia-mock 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@deanwhillier/jest-matchmedia-mock 可以商用吗?

@deanwhillier/jest-matchmedia-mock 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deanwhillier/jest-matchmedia-mock 的下载量有多少?

根据 npm 官方下载统计,@deanwhillier/jest-matchmedia-mock 在 2026/10/12 – 2026/11/10 这 30 天内被下载 28,773 次,日均约 959 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deanwhillier/jest-matchmedia-mock 还在维护吗?

最新版本 1.2.0 发布于 2021/12/03(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/12/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。