JestのWebブラウザーAPIでのテスト環境設定

develop/JavaScript
概要

JavaScriptの人気テストフレームワークJest | GNU social JP」で記した通り、Jestの環境を用意しました。実際に最初のテストコードを作成して、npm run testで実行したところ、エラーが発生しました。その対応を記します。Jest v29.5で確認しました。

エラーは以下でした。

 FAIL  js/misc-functions.test.js
  ● Test suite failed to run

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment. ReferenceError: window is not defined 887 | · · · · · · · · · */ 888 | > 889 | window.userArrayCache = new Object();

どうやら、Webブラウザーのwindowオブジェクトを参照できずにエラーになっています。

設定

エラーにあるように、jsdomというテスト環境を使うとよいらしいです。「Configuring Jest · Jest」に記載があります。

この投稿は Gold and Silver の会員限定です。記録が目的であり、元投稿の削除・炎上回避、運営費・GNU socialの開発資金調達などのため会員限定にしています。

このサイトでは分散SNSの話題を1年以上ほぼ毎日更新しています。最新動向・詳細情報を把握したい人は、会員登録/ログインをお願いします (About Member)。

Comments

Copied title and URL