![[Git] Automatically running tests before commits with ghooks [Git] Automatically running tests before commits with ghooks](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
Wouldn't it be nice if everyone ran the tests before committing code? With ghooks, you can automatically add a githook when dependencies are installed which will allow you to define common scripts to be run at various points during git actions (like committing). In this lesson, we'll add a githook for running the tests before we commit code to make sure we don't commit anything that breaks the tests.
Install:
npm i -D ghooks cross-env
[Notic]: cross-env is a package which enable you write Linux/mac scripts in your package.json and the same script can also run on Windows machine.
Config:
// package.json "config": {
"ghooks": {
"pre-commit": "npm run test:single",
"post-merge": "npm install"
}
}
So before commit , it runs tests first and after merged, run npm install.