Documentation is pretty sparse on doing coverage with istanbul for integration tests based on this post. 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
swagger-tests
#createPet
โ should add a new pet (15226ms)
#getPets
โ 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)
#getPetsSortedByName
โ should be sorted by name (85822ms)
#deletePet
โ should delete a pet (159ms)
9 passing (2m)
No coverage information was collected, exit without writing coverage information
When I run istanbul report, it has nothing to report on nor generates any useful files. The post author and I tried to troubleshoot but to no avail. Any idea?
Here's my project (use develop branch) to reproduce the issue.
OK, what you want to do is to track coverage for the running server code when you hit it with test endpoints right?
Just be aware of what code is getting instrumented and reported on (the server-side code) and istanbul should work for you (unfortunately with an option that is somehow not documented)
What you need is this sequence (there is no need to pre-instrument the code). I'll show you how I did this manually and you can figure out how to automate it:
Step 1: Run your server under istanbul
./node_modules/.bin/istanbul cover --handle-sigint server.js
This runs the server under istanbul cover and instructs istanbul to handle interrupts (Control-C) and dump the coverage information + reports when the process exits.
Step 2: Run your tests, no need to run under istanbul
$ ./node_modules/.bin/_mocha -R spec ./.instrument/test/** --recursive
Step 3: Terminate the server with a Control-C (I did this by hand), and you should see:
=============================================================================
Writing coverage object [/Users/ananthk/open-source/swagger-rest/coverage/coverage.json]
Writing coverage reports at [/Users/ananthk/open-source/swagger-rest/coverage]
=============================================================================
=============================== Coverage summary ===============================
Statements : 72.29% ( 120/166 )
Branches : 66.67% ( 34/51 )
Functions : 78.05% ( 32/41 )
Lines : 72.29% ( 120/166 )
================================================================================
Step 4: Open the HTML coverage report under coverage/lcov-report/index.html
Step 5: Profit!
Yes, that switch did the trick. In step 2, I just went after the tests instead of the ones instrumented since you suggested I didn't need to do that. Thank you!
$ ./node_modules/.bin/_mocha -R spec ./test/** --recursive
Any advice for getting this to work if your server-side code involves coffeescript? I only get coverage results for my js code, but I want to include my coffee code too. Thanks
OK, what you want to do is to track coverage for the running server code when you hit it with test endpoints right?
Just be aware of what code is getting instrumented and reported on (the server-side code) and istanbul should work for you (unfortunately with an option that is somehow not documented)
What you need is this sequence (there is no need to pre-instrument the code). I'll show you how I did this manually and you can figure out how to automate it:
Step 1: Run your server under istanbul
./node_modules/.bin/istanbul cover --handle-sigint server.jsThis runs the server under
istanbul coverand instructs istanbul to handle interrupts (Control-C) and dump the coverage information + reports when the process exits.Step 2: Run your tests, no need to run under istanbul
$ ./node_modules/.bin/_mocha -R spec ./.instrument/test/** --recursiveStep 3: Terminate the server with a Control-C (I did this by hand), and you should see:
============================================================================= Writing coverage object [/Users/ananthk/open-source/swagger-rest/coverage/coverage.json] Writing coverage reports at [/Users/ananthk/open-source/swagger-rest/coverage] ============================================================================= =============================== Coverage summary =============================== Statements : 72.29% ( 120/166 ) Branches : 66.67% ( 34/51 ) Functions : 78.05% ( 32/41 ) Lines : 72.29% ( 120/166 ) ================================================================================Step 4: Open the HTML coverage report under
coverage/lcov-report/index.htmlStep 5: Profit!
This solved my problem - almost. I still see that this doesn't produce report for all files in the repo. In another post I saw that Istanbul requires files to be required explicitly to produce a report with all files. But I see that event if , in this case - server.js - has require() for all files that I have, Istanbul ignores them if there is not a call to those files.
Is there a way to get around this issue?
@saranyasivaraj I used concurrently + wait-on to run a server plus a test process, calling the script using nyc seems to do the trick. see: https://github.com/istanbuljs/nyc/issues/548#issuecomment-634453746
Most helpful comment
OK, what you want to do is to track coverage for the running server code when you hit it with test endpoints right?
Just be aware of what code is getting instrumented and reported on (the server-side code) and istanbul should work for you (unfortunately with an option that is somehow not documented)
What you need is this sequence (there is no need to pre-instrument the code). I'll show you how I did this manually and you can figure out how to automate it:
Step 1: Run your server under istanbul
This runs the server under
istanbul coverand instructs istanbul to handle interrupts (Control-C) and dump the coverage information + reports when the process exits.Step 2: Run your tests, no need to run under istanbul
Step 3: Terminate the server with a Control-C (I did this by hand), and you should see:
Step 4: Open the HTML coverage report under
coverage/lcov-report/index.htmlStep 5: Profit!