I’m sure that many of you, who are reading this post, had the situation: you have a great sunny morning, birds are singing, you come to the office, pull the latest from the master and application is broken and that’s it — your morning is ruined 😡.

There is a great tool to eliminate this problem — husky. Husky makes adding git hooks as easy as it can be.

Hooks are programs you can place in a hooks directory to trigger actions at certain points in git’s execution. — from git documentation.

With husky you just need to provide a script in your package.json file and the script will be triggered on commit or push, for example.

Install husky

npm install -D husky

2. Add any hook to your package.json

"precommit": "your code goes here (testing, linting)..."

In my opinion prepush command is the most useful, as it is not executed as often as precommit, and it still makes sure that the broken code is not pushed to the master.

Below is the config that our team is using on the current Angular project:

"prepush": "run-p lint test-headless \"ng -- build --prod\" "

The code above runs linting (“lint”: “ng lint”), runs unit tests (“test-headless”: “ng test -sr — browsers ChromeHeadless”) and checks that production build does not fail (\”ng — build — prod\”). I’m also using npm-run-all to execute all tasks in parallel.