Current Project: wordapi.net.

I’ve made solid progress over the last few weeks and have become accustomed to writing unit tests before implementing a new method or module. The API is shaping up quite nicely and the test-driven approach to development feels great. The final step for this project in regards to TDD is learning about coverage. In this post, I’ll discuss how I set up Istanbul for code coverage.

Code coverage tools like Istanbul measure the amount of our code covered by tests. They produce reports detailing the number of statements, functions and lines covered under our unit tests. They can also tell us the code that isn’t covered, allowing us to further enhance our tests to satisfy all conditions. Having coverage tools at our dispense can give us further confidence in our code.

=============================== Coverage summary ===============================
Statements   : 97.77% ( 175/179 )
Branches     : 89.13% ( 82/92 )
Functions    : 96.97% ( 32/33 )
Lines        : 97.71% ( 171/175 )
================================================================================

To make Istanbul work with Babel, I needed to install an alpha version of the package. This was due to issues with the default installation, where the coverage seemed to work as expected but no actual information was collected:

No coverage information was collected, exit without writing coverage information

I uninstalled the default installation and replaced it with this:

npm install istanbul@1.1.0-alpha.1 --save-dev

Then I updated my package.json file to add the script that would run the code coverage:

"scripts": {
  "cover": "istanbul cover _mocha test/main.test.js -- --compilers js:babel-core/register"
},

Note For some reason, coverage would only work using _mocha vs mocha. I found out later that _mocha is the actual executable that runs Mocha and the default mocha command is just a wrapper around it. The --compilers js:babel-core/register tells Mocha to run with ES6 compatibility.

I could now test my coverage using npm run cover. This produced a coverage folder in the root of my project, with a HTML file containing the coverage report:

Thanks for reading! Please ask questions or leave comments in the box below, and if you’d like to follow the project, please click on this link: https://github.com/lyndseybrowning/wordapi.net.