0
1

More than 1 year has passed since last update.

git push前にユニットテストを自動実行させる

Posted at

背景

テスト実行せずpushしてmaster(main)ブランチにエラーが混じるケースがあった

対処

huskyを導入し、push前にユニットテストを実行させる
https://github.com/typicode/husky

実装

huskyのインストール
※npmの場合

npm install husky -D

hookファイル作成

npx husky add .husky/pre-push "npm test"

ちなみにcommit時に実行させたい場合は pre-commit に記述

npx husky add .husky/pre-commit "npm test"

.gitファイルとpackage.jsonが同一ファイルにない場合

こんなディレクトリ構成の場合

root/
|--.git/
|--web/
|  └──...
└──client/
   |--...
   |--package.json
   └──...

.git can't be foundというエラーが出る。

対処として、まずはpackage.json内のhuskyを呼び出すscriptにディレクトリ移動を含める

"scripts": {
  "prepare": "cd .. && husky install client/.husky"
}

hookファイル内でもディレクトリ移動を指定する。

npx husky add .husky/pre-push "cd client && npm test"

結果

git pushコマンドを打つと、自動でユニットテストが走るようになりました。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1