Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

テスト実行環境をVitestに移行 #78

Merged
merged 11 commits into from Jan 24, 2024
Merged

Conversation

keitakn
Copy link
Member

@keitakn keitakn commented Jan 23, 2024

issueURL

#77

この PR で対応する範囲 / この PR で対応しない範囲

#77 の完了の定義に記載されている通りテストの実行環境をVitestに移行する。

Storybook の URL、 スクリーンショット

変更点概要

テスト実行環境をVitestに移行。Jest周りのpackageや設定ファイルは全て削除。

基本的には以下の内容を確認しながら対応を実施したがテストコードにはあまり変更する箇所はなかった。

https://blog.stackademic.com/migration-from-jest-to-vitest-is-it-worth-it-react-ts-4ce220f6bfcf

また以下の記事も参考にさせて頂いた。

コードカバレッジがかなり低下してるがこれは、coverage.exclude の設定が上手く動作していないのが原因。

ここは別のPRで最適な設定を探す予定。

Vitestに変更した事でJestで発生していたJestのプロセスが終了しない問題は解決済。

レビュアーに重点的にチェックして欲しい点

特になし。

補足情報

インラインコメントに記載。

Copy link

vercel bot commented Jan 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ai-cat-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 24, 2024 2:35pm

@keitakn keitakn changed the title Feature/issue77/vitest テスト実行環境をVitestに移行 Jan 24, 2024
Copy link

codecov bot commented Jan 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e9c42ca) 69.41% compared to head (a580f5b) 11.67%.

Additional details and impacted files
@@             Coverage Diff             @@
##             main      #78       +/-   ##
===========================================
- Coverage   69.41%   11.67%   -57.74%     
===========================================
  Files          18       68       +50     
  Lines          85     2090     +2005     
  Branches       10       69       +59     
===========================================
+ Hits           59      244      +185     
- Misses         26     1796     +1770     
- Partials        0       50       +50     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +10 to +11
vitest.config.mts
vitest.setup.mts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.mts のファイルをESLintにかけるとエラーになるのでESLintの対象外にした。(設定ファイルの為だけに .mts をESLintで利用出来るようにするのはコストが高いと判断)

@@ -41,6 +41,7 @@
},
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^4.1.1",
"@next/env": "^14.1.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以前は next/jest を使っていたので環境変数をテスト内でも利用出来ていたがVitestになった事でそれがなくなったので @next/env を追加

@@ -50,29 +51,31 @@
"@storybook/nextjs": "^7.6.6",
"@storybook/react": "^7.6.6",
"@storybook/testing-library": "^0.2.2",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/jest-dom": "^6.2.1",
Copy link
Member Author

@keitakn keitakn Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@testing-library/jest-dom は名前的にJest専用に見えるがVitestでも利用出来る。

これがあると toBeInTheDocument のような便利なカスタムマッチャーが利用出来るので引き続きこれを利用する。

"@testing-library/react": "^14.1.2",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@vitejs/plugin-react-swc": "^3.5.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitejs/plugin-react-swc これは少しでもテスト実行時のBuild時間を短縮する為に追加。

"@testing-library/react": "^14.1.2",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"@vitest/coverage-v8": "^1.2.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitest/coverage-v8 コードカバレッジを出力する為に追加。

"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"eslint-plugin-vitest": "^0.3.20",
"jsdom": "^24.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy-dom という次世代のライブラリが出ていますが以下のようにまだ一部問題があるので今回は定番の jsdom を利用。

capricorn86/happy-dom#1135

"msw": "^2.0.11",
"msw-storybook-addon": "^2.0.0--canary.122.b3ed3b1.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.1.1",
"storybook": "^7.6.6",
"whatwg-fetch": "^3.6.20"
"vite-tsconfig-paths": "^4.3.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vite-tsconfig-pathstsconfig.json のパスを vitest.config.mts に設定する為に追加。

@@ -1,6 +1,7 @@
import '@testing-library/jest-dom/vitest';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@testing-library/jest-dom/vitestvitest.setup.mts に記載されているのでここに書く必要はないがIDE上で型エラーになってしまうのでここに追加。

root: './',
environment: 'jsdom',
setupFiles: ['./vitest.setup.mts'],
reporters: ['default', 'hanging-process'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hanging-process はStreamingのテストが途中でハングアップしてしまう場合があるのでデバッグ用に追加。

@keitakn keitakn marked this pull request as ready for review January 24, 2024 15:54
@keitakn keitakn merged commit f02e7b5 into main Jan 24, 2024
8 of 9 checks passed
@keitakn keitakn deleted the feature/issue77/vitest branch January 24, 2024 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant