Anubhav Dhawan Anubhav Dhawan - 1 year ago 63
Node.js Question

How to get coverage report for external APIs?

I'm trying to get coverage report for the API code. I have all the test cases running perfectly in mocha. My problem is that my API server and the test cases are written in separate repositories.

I start my node API server on localhost, on a particular port, and then using supertest in mocha, hit the localhost url to test server's response.

Can you suggest me the best way to generate a coverage report for those APIs?

Answer Source

Testing env

If you want to get coverage, supertest should be able to bootstrap the app server, like in the express example.

The drawback is that you must not run your tests against a running server, like

var api = request('');

but you must include your app entrypoint to allow supertest to start it like

var app = require('../yourapp'); 
var api = request(app);

Of course, this may (or may not) result in a bit of refactoring on your app bootstrap process.

As other options, you can use node CLI debug capabilities or use node-inspector.

Coverage setup

Supposing you are willing to install istanbul in association with mocha to get coverage.

npm install -g istanbul


istanbul cover mocha --root <path> -- --recursive <test-path>
  • cover is the command use to generate code coverage
  • mocha is the executable js file used to run tests
  • --root <path> the root path to look for files to instrument (aka the "source files")
  • -- is used to pass arguments to your test runner
  • --recursive <test-path> the root path to look for test files

You can then add --include-all-sources to get cover info on all your source files.

In addition you can get more help running istanbul help cover

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download