CRAで作られたReactアプリにStoryshotsを導入する
バックエンドだけでなくフロントエンドも最近はテストを導入しようという流れが出てきてます。
今回はstoryshotsを用いたテストのスナップショットテストの導入について書きます。
storyは作られてる前提とします。これから説明しますが、storyがあれば導入の手間はあまり掛からないです。
環境
yarn -v
1.22.10
node -v
v14.11.0
@storybook/[email protected]
などなど
入れる手順
storyshotsのインストール
yarn add -D @storybook/addon-storyshots
下記を追加
import initStoryshots from '@storybook/addon-storyshots';
initStoryshots();
yarn test
を実行すると、以下のようにテストが実行され、
__snapshots__
以下にファイルが作成されました。
スナップショットの更新
コンポーネントに変更を加えた場合、保存してあるスナップショットとの差分が出るので、テストが落ちるようになるかと思います。
正しい動きをしている場合はスナップショットを更新する必要があります。
スナップショットの更新は以下のコマンドです。
yarn test --updateSnapshot
ハマったポイント
テストを実行しようとしたときに下記のエラー文に遭遇しました。
$ yarn test
yarn run v1.22.10
$ react-scripts test
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.1.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
/Users/hideokaizuka/dev/PrAhaChallenge/test/storybook/my-app/node_modules/babel-loader (version: 8.2.2)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /Users/hideokaizuka/dev/PrAhaChallenge/test/storybook/my-app/node_modules/babel-loader is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls babel-loader in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-loader.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
また,
npm list --depth=0
パッケージのバージョンを確認したときにも下記のようになりました。
npm list --depth=0
[email protected] /Users/hideokaizuka/dev/PrAhaChallenge/test/storybook/my-app
├── UNMET PEER DEPENDENCY @babel/core@^7.9.6
├── @babel/[email protected] extraneous
├── @babel/[email protected] extraneous
├── @babel/[email protected] extraneous
├── @emotion/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @storybook/[email protected]
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
npm ERR! peer dep missing: @babel/core@^7.9.6, required by @storybook/[email protected]
///長すぎたので割愛
何かパッケージの依存関係に問題があるよう。
解決策
yarn test
を実行したときにエラー文に下記のように書いてある。
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
なので、yarn-lock
とnode_modules
を削除してからyarn install
を実行したところ解決した。
似たような問題に当たっている人の助けになれば幸いです。
参考
関連
少し発展の記事
Author And Source
この問題について(CRAで作られたReactアプリにStoryshotsを導入する), 我々は、より多くの情報をここで見つけました https://qiita.com/hideokaizuka/items/2d7b636b5255e2091b70著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .