LoginSignup
22
3

More than 3 years have passed since last update.

node/js向け husky使ってpush前にテストするを共有する

Last updated at Posted at 2019-12-22

テストや静的コード解析実行前にうっかりpushしてしまう事はないでしょうか?

有意義レビューの為にも事前にテストを実行し、リモートにはテスト通ったコードしか置かないようにしたいですね。

githooksのpre-pushを利用したpush前に必ずテストし、失敗したコードはpushできない状態を作る事ができます。

githooksのpre-pushを利用したpush前テスト

.git/hooks/pre-push に実行したいコマンドを書きます。 pre-pushgit push の前に実行されるフックです。

ほとんどのテストコマンドは、すべて通った場合に終了コード 0 を、失敗した場合に 1 などの0以外を返すようになっています。

0 の場合にのみ git push が実行できるようになります。

$ cp .git/hooks/pre-push.sample .git/hooks/pre-push
$ chmod +x .git/hooks/pre-push
[.git/hooks/pre-push]
#!/bin/sh

yarn test

ただ、この方法ではプロジェクトメンバーへの共有が難しくなります。テンプレ作って pre-push 書き換えてと案内するのは面倒ですね。

husky使ったpush前テスト

husky をinstallして package.json にpush前に実行したいコマンドを書くだけです。(.huskyrc を用意しても良いです)

$ yarn add --dev husky

pre-commit などのhookも用意されています。コミット単位でテストするのは非効率なので pre-push を使います。

package.json
{
  ...
  "husky": {
    "hooks": {
      "pre-push": "yarn test"
    }
  },
  "devDependencies": {
    "husky": "^3.1.0"
  }
}

push時に自動でテストが走り、失敗するとpushできないようになりました。

$ git push

husky > pre-push (node v10.14.1)
yarn run v1.17.3
$ yarn test
...
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky > pre-push hook failed (add --no-verify to bypass)

プロジェクトメンバーはパッケージインストール時にpre-pushが書き換わる

clone時のhooksは何も登録されていないsample状態ですが、

$ git clone xxxxx
$ ls .git/hooks
applypatch-msg.sample     fsmonitor-watchman.sample pre-applypatch.sample     pre-push.sample           pre-receive.sample        update.sample
commit-msg.sample         post-update.sample        pre-commit.sample         pre-rebase.sample         prepare-commit-msg.sample

パッケージインストール後に書き換えてくれます。

$ yarn
yarn install v1.21.1
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 2.29s.

$ ls .git/hooks
applypatch-msg            fsmonitor-watchman.sample post-merge                post-update.sample        pre-commit                pre-push.sample           pre-receive.sample        sendemail-validate
applypatch-msg.sample     post-applypatch           post-receive              pre-applypatch            pre-commit.sample         pre-rebase                prepare-commit-msg        update
commit-msg                post-checkout             post-rewrite              pre-applypatch.sample     pre-merge-commit          pre-rebase.sample         prepare-commit-msg.sample update.sample
commit-msg.sample         post-commit               post-update               pre-auto-gc               pre-push                  pre-receive               push-to-checkout

まとめ

必ず実行するであろうパッケージインストール後に書き換えてくれるので勝手に共有できるのは良いですね。

githooks管理ツールは他にもpre-commitovercommitなどがありますが、 husky はシンプルで良い感じです。

22
3
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
22
3