Git hooks are basically scripts fired before an important action occurs, eg: before a commit is made, before code is pushed to a repo after a commit i

Automate JavaScript Code Linting and Formatting in 5 simple Steps

submited by
Style Pass
2021-08-18 16:00:10

Git hooks are basically scripts fired before an important action occurs, eg: before a commit is made, before code is pushed to a repo after a commit is made, etc. You can learn more about Git Hooks and the different kinds of hooks over here

We will use mocha and chai for testing. We will write a single test case for each of our functions. To learn more about mocha and chai in-depth, refer to this article.

You will get a bunch of questions, answer them based on your project. Finally, we will add a new command inside ‘scripts’ in ‘package.json’ You can add this under the ‘test’ command we added in the previous section.

This will run the linter on all your javascript files and fix the linting errors wherever it can. You can also disable some es-lint checks by adding comments to the top of the file or above certain lines. For example, I disabled a couple of checks in the ‘tester.js’ file

If the linter returns an error or one of the test cases fails, the commit will be unsuccessful and your files will remain in the staging area and not be committed.

Leave a Comment