Angular: How to force run unit tests when running Git push?

Viewed 4841

I have seen Angular projects in which the unit tests are run every time a build is executed and also when running the git push command. If any tests fail during either command, the process does not execute until all your unit tests pass or unless you bypass. I would like to have this kind of set up for my project as best practice. Please help :)

3 Answers

To run builds, unit tests, etc before a commit or a push you can use a tool like Husky.

Git provides a methodology to hook it's events using .git/hooks

you can add a folder to your project called .git/hooks and within that folder add a subfolder called pre-commit and within that you may place scripts that are to be ran. This being whatever your test command is.

For example test.sh would contain: ng test

More documentation about hooking git events can be found here: https://git-scm.com/docs/githooks

Let me know if you have any questions, I would be happy to revise my answer!

Add a package called husky as a devDependencies.

npm i husky --save-dev

Now catch the pre-commit webhook by using husky, to do that in your package.json, add this object:

"husky": {
   "hooks": {
       "pre-commit": "ng test"
   }
}

This will make sure that every time you make a commit, ng test runs before the actual commit happens.

Related