Adding Test Coverage to your NodeJS app with Istanbul, TravisCI, and Codecov

Erick Zhao
2 min readOct 22, 2017

A little background: over the past couple weeks, I’ve been working on invite-contributors, a GitHub app that leverages the Probot framework to allow organizations to automatically invite new contributors into their organization.

It’s actually my first foray into deploying a Node app, and I wanted to be like the cool kids and make sure my code quality was adequate by writing tests (even though I currently have <100 LOC in total).

I used Mocha and Expect to test my code (they’re the defaults for Probot), and decided I wanted to include a test coverage reporter into my continuous integration flow (which was deploying to Heroku whenever my TravisCI build passed).

I decided to use Codecov.io to integrate code coverage with my GitHub repo based on the fact that it had a cute browser extension that integrates into GitHub (didn’t really think to much about it). Codecov in turn recommended Istanbul to generate coverage reports locally.

To actually run Istanbul, I used nyc (Istanbul’s official CLI), which integrates well with Mocha (or any other test runner, so I’ve heard). Running tests with Mocha and getting a coverage report with nyc is as easy as running nyc mocha .

I also decided to split my Travis build into two different scripts in my package.json :

  1. test runs all Mocha tests locally and reports how much LoC coverage you have (entirely locally).
  2. report-coverage packages the report and sends the information to Codecov.
// package.json
...
"scripts": {
"test": "nyc mocha",
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
}
...

…in the .travis.yml , simply add both scripts to your build:

// .travis.yml...
script:
- npm run test
- npm run report-coverage
...

…and don’t forget to add reports generated by nyc to your .gitignore :

// .gitignore...
.nyc_output
coverage
*.lcov
...

This works pretty smoothly for me so far. Now, I can still run npm test to make sure that new code functions as intended, and every Travis build sends updated code coverage information to Codecov.

--

--

Erick Zhao

Software Engineering student from Montreal. I like making stuff on the web.