Istanbul — JavaScript Code Coverage

Kishan Chaitanya
3 min readAug 3, 2020

--

JavaScript Code Coverage Tool

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.

Code Coverage Formats

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.

HTML Output

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.

Code Coverage Types

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.

Istanbul integrated with SonarCloud, SonarScanner and GitHub

If you would like to know on how to integrate all these, Please feel free to message me. Happy to walk you through this.

--

--

Kishan Chaitanya
Kishan Chaitanya

Written by Kishan Chaitanya

8+ years of professional software development experience focused on scalable web applications.

No responses yet