Adding Git hooks for tests
A Git hook is a method that fires on common Git commands. We can use this method to invoke custom scripts when we commit or push our code. It is common practice to use these hooks to run checks against your repository to ensure that the code that is being added does not break application functionality. One valuable check we could add is to run our applications unit tests before we push
our code, and if they fail, we can stop the push. By implementing this feature, it's unlikely that the code being pushed will break any functionality we test for.
Let's implement this functionality now by creating a Git hook that is triggered by a git push
. This will ensure our unit tests pass before allowing the push
command to run. We will be using the husky
package to do this as it is easy to set up and maintain:
- Install the necessary dependencies:
npm install husky --save-dev
- Create a
postinstall
script in yourpackage.json
file with the following...