occasl occasl - 10 months ago 51
Node.js Question

Using Istanbul for integration tests against a Node microservice

Documentation is pretty sparse on doing coverage with istanbul for integration tests. When I run through my mocha tests, I get

No coverage information was collected, exit without writing coverage information

The first thing I do is instrument all my source code:

✗ istanbul instrument . -o .instrument

In my case, this is a REST microservice that is Dockerized which I have written Mocha tests to run against it to validate it once it is deployed. My expectation is istanbul will give me code coverage against the source from that Node service.

The second step I do this command to run node on my instrumented code:

✗ istanbul cover --report none .instrument/server.js

After that, I run my tests using the following from the my main src directory as follows (with results):

✗ istanbul cover --report none --dir coverage/unit node_modules/.bin/_mocha -- -R spec ./.instrument/test/** --recursive

✓ should add a new pet (15226ms)
✓ should exist and return an Array (2378ms)
✓ should have at least 1 pet in list (2500ms)
✓ should return error if search not name or id
✓ should be sorted by ID (3041ms)
✓ should be sorted by ID even if no parameter (2715ms)
✓ should be only available pets (2647ms)
✓ should be sorted by name (85822ms)
✓ should delete a pet (159ms)

9 passing (2m)

No coverage information was collected, exit without writing coverage information

When I run
istanbul report
, it obviously has nothing to report on.

What am I missing?

See develop branch of this project to reproduce issue.

Answer Source

The owner of istanbul help me resolve this. I was able to get this going by following these steps:

  1. Skip instrumenting the code; it's not needed
  2. Call istanbul with --handle-sigint as @heckj recommended and remove the flag --report none
  3. Once your server is up, just run tests as normal: ./node_modules/.bin/_mocha -R spec ./test/** --recursive
  4. Shutdown the server from step 2 to output the coverage
  5. View the HTML file in open coverage/lcov-report/index.html