背景
Reactアプリのレンダリングについてのテストとして、Jestのスナップショット機能を使用している。
テスト実行時に「Jest Could not locate module FileName.css :mapped as identity-obj-proxy」というエラーメッセージが出力されたのでその解決策をまとめた。
※「FileName.css」にはプロジェクトに存在するCSSファイル名が入る。
> myProject@0.0.1 test C:\Users\admin\Documents\myApp
> jest
FAIL _tests_\Home.test.js
Test suite failed to run
Configuration error:
Could not locate module react-datepicker/dist/react-datepicker-cssmodules.css (mapped as identity-obj-proxy)
Please check your ○○ for these entries:
{
"moduleNameMapper": {
"/^.+\.(css|less)$/": "identity-obj-proxy"
},
"resolver": undefined
}
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.894s
Ran all test suites.
npm ERR! Test failed. See above for more details.
開発言語 (バージョン情報)
- React.js (17.0.2)
- Next.js (11.1.2)
- TypeScript (4.4.3)
- Jest (27.2.2)
解決策
ターミナルで以下のコマンドを実行する。ただし実行はpackage.jsonのあるディレクトリでおこなう。
npm install --save-dev identity-obj-proxy