Istanbul — JavaScript Code Coverage
Code Coverage is really important to tell how well you code is been testing and really gives you the confident that your recent changes do not break any existing functionality.
Code Coverage also tells you any unused code present in your repository, So removing that code helps you in avoid any unexpected application behavior in the future.
While there are couple of code coverage tools available, One tool really stood out with ample support of coverage formats is Istanbul.
Each output can be used independently or combined with other tools along the product development.
HTML output can be used independently to view the outcome in any browser.
Some other formats like lcov, json can be used with other tools in your application development life cycle.
Istanbul provides multiple types of coverage results like Statement, Branch, Function and Line Coverage. While usually Line Coverage is preferred since it gives you complete line by line coverage.
This tool could be easily installed using the below command
yarn add nyc
Once you install this tool using any node package manager. You would need to add a config file called .nycrc
{
"all": true,
"report-dir": "test/coverage/",
"reporter": [
"text",
"lcov"
],
"exclude": [
"config",
"coverage",
"migrations",
"scripts",
],
"extension": [".ts"]
}
Each config services a purpose,
all to true means to include all the files to consider for code coverage
report-dir is the location where the code coverage report to be stored
reporter is the type of reporters you would like to see the coverage
exclude means to tell which files/directories to exclude from coverage
extension means to consider other type of extension other than javascript
Once the setup is ready, you could run Istanbul with your test runner
nyc mocha
Istanbul could also be combined with tools like SonarCloud, SonarScanner and GitHub to report coverage and also enforce quality checks to ensure that the developers right unit tests along with the development code. Below is the sneak peek on how all this could be integrated.
If you would like to know on how to integrate all these, Please feel free to message me. Happy to walk you through this.