CRAで作られたReactアプリにStoryshotsを導入する

バックエンドだけでなくフロントエンドも最近はテストを導入しようという流れが出てきてます。
今回は[storyshots](https://github.com/storybookjs/storybook/tree/master/addons/storyshots/storyshots-core)を用いたテストのスナップショットテストの導入について書きます。

storyは作られてる前提とします。これから説明しますが、storyがあれば導入の手間はあまり掛からないです。


## 環境

`yarn -v
1.22.10`

`node -v
v14.11.0`

`react@16.13.1`

`@storybook/react@6.1.20`

などなど

## 入れる手順
storyshotsのインストール

`yarn add -D @storybook/addon-storyshots`

下記を追加

```src/Storyshots.test.js
import initStoryshots from '@storybook/addon-storyshots';
initStoryshots();
```

`yarn test`を実行すると、以下のようにテストが実行され、

![storyshots.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/307699/85be540e-78a8-7744-065e-51402523cea6.png)

`__snapshots__`
以下にファイルが作成されました。

## スナップショットの更新

コンポーネントに変更を加えた場合、保存してあるスナップショットとの差分が出るので、テストが落ちるようになるかと思います。

![スナップショットテスト落としたときのやつ.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/307699/5e1bb478-6dfb-e022-8c8b-ac4a675fdbde.png)

正しい動きをしている場合はスナップショットを更新する必要があります。


スナップショットの更新は以下のコマンドです。

`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
my-app@0.1.0 /Users/hideokaizuka/dev/PrAhaChallenge/test/storybook/my-app
├── UNMET PEER DEPENDENCY @babel/core@^7.9.6
├── @babel/plugin-syntax-typescript@7.8.3 extraneous
├── @babel/plugin-transform-flow-strip-types@7.9.0 extraneous
├── @babel/plugin-transform-runtime@7.9.0 extraneous
├── @emotion/react@11.1.5
├── @storybook/addon-actions@6.1.20
├── @storybook/addon-essentials@6.1.20
├── @storybook/addon-links@6.1.20
├── @storybook/addon-storyshots@6.1.21
├── @storybook/node-logger@6.1.20
├── @storybook/preset-create-react-app@3.1.6
├── @storybook/react@6.1.20
├── @testing-library/jest-dom@4.2.4
├── @testing-library/react@9.5.0
├── @testing-library/user-event@7.2.1
├── @types/jest@24.9.1
├── @types/node@12.12.34
├── @types/react@16.9.32
├── @types/react-dom@16.9.6
├── react@16.13.1
├── react-dom@16.13.1
├── react-scripts@3.4.1
└── typescript@3.7.5

npm ERR! peer dep missing: @babel/core@^7.9.6, required by @storybook/addon-essentials@6.1.20

///長すぎたので割愛
```

何かパッケージの依存関係に問題があるよう。

解決策

`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`を実行したところ解決した。

似たような問題に当たっている人の助けになれば幸いです。


## 参考

https://storybook.js.org/docs/react/workflows/snapshot-testing

https://github.com/storybookjs/storybook/tree/master/addons/storyshots/storyshots-core

https://helen.hatenablog.com/entry/2016/05/22/003057

https://blog.gaji.jp/2020/08/18/4783/

## 関連
少し発展の記事

https://qiita.com/hideokaizuka/items/a5ad7ade26d6d50d76ae