2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Chakra UI v3をJestでテストしたらTypeError: window.matchMedia is not a function

Posted at

はじめに

chakra ui v3をJestでテストした際に起きたエラーです。

問題

テストを実行すると、TypeError: window.matchMedia is not a functionとwindowオブジェクトの matchMedia関数が無いと指摘されます。

解決方法

jest.setup.tsに下記を追加します。

jets.setup.ts
global.matchMedia =
  global.matchMedia ||
  function () {
    return {
      matches: false,
      addListener: jest.fn(),
      removeListener: jest.fn(),
    };
  };

一言でいうと、matchMedia がデフォルトで用意されていなければ、何もしないような関数に置き換えるということです。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?