0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jestでライブラリからwindow.URL.createObjectURLのエラーが出たときの対処法

Posted at

Jestでフロントのテストを書いている際に、下記のようなエラーが出ました。

スクリーンショット 2023-05-11 232936.jpg

調べると、モックを使って解消する方法は簡単に見つかると思いますが、
今回はライブラリが引っかかってしまうケースです。

なので、こういう場合は対象のテストファイルではなく
セッティングファイルに記述します。

reactなら、src配下にあるsetupTests.tsです。

src/setupTests.ts
// window.URL.createObjectURLを呼び出すが、node.js上では利用できないのでモック関数と追加する
window.URL.createObjectURL = function () {
  // 型に対応するためにstringを返す
  return '';
};

他にもwindow.HTMLCanvasElementでもテスト時にエラーが出ましたが、
同じようにsetupTests.tsにモックを作って解消できます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?