How To Test Code Before Pushing To The Repository

Are you truly ready to push your code to repository? Or you still thinking on how to test it before?
tl;dr
- it is good to test the code before pushing to the repo,
- you can setup automatic testing with GIT Hooks, but it is complicated,
- there is a really easy way for testing before committing changes, it is called pre-commit,
As a professional developer you need to make sure your code is working, tested and it has been written correctly: easy to read and understand and compliant to team coding standards. As a smart developer, you can help yourself and set up automatic pre-commit checking, you can use it for checking:
- code test coverage,
- SCSS style guide compliance,
- JS style guide compliance.
Step 1: Install pre-commit npm module
There are some ways to setup GIT hooks, but it can be a bit complicated, and you need to do the same job for every repository.
Much easier way is to use npm module called pre-commit, it creates GIT hooks automatically.
Let’s try that:
npm install --save-dev pre-commit
Step 2: Setup testing script
Ok so we have the tool for automatic testing, now we need to define a test script. In our case, it will be checking JavaScript Standard Style compliance in all files inside src/js folder.
Let’s add the script in package.json file:
"scripts": {
"lint": "standard 'src/js/**/*.js'"
}
It is good to test what we’ve already done so let’s run the script manually to check if it’s working:
npm run lint;
Step 3: Adding a test script to pre-commit
Ok! When the testing script is created we need to add it to the pre-commit configuration (also in package.json file):
"pre-commit": [
"lint"
],
Step 4: Let’s give a try
Once everything is set up, you can try to do the commit. Pre-commit hook should automatically do the testing and prevent you from committing in case problems with the code.

pre-commit test run pre-commit blocked the commit because of JavaScript issues in source code
Thanks nante 😉