It's a good practice to run the test suite before committing to git
and only allowing the commit to pass if the tests have been executed successfully. The same applies for other version control systems.
To achieve this, we should add the .git/hooks/pre-commit
file, which should take care of running the tests and exiting with an error in case they failed. Luckily, this is a repetitive task (which can be applied to all Node applications), so there is an NPM module that creates this hook file for us. All we need to do is install the pre-commit
module (https://www.npmjs.org/package/pre-commit) as a development dependency using the following command:
$ npm i pre-commit ––save-dev
This should automatically create the pre-commit
hook file so that all the tests are run before committing (using the npm test
command).