gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 785 forks source link

Istanbul refuses to generate coverage report even after requiring other files #907

Open iamthadiyan opened 5 years ago

iamthadiyan commented 5 years ago

My starting point was the answer to Issue #574

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!

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 for files that are not executed. But I see that event if , in this case - server.js - has required all files that I have, Istanbul ignores them if there is not a call made to those files.

Is there a way to get around this issue?

rahul9222 commented 5 years ago

Using the above example it generate the proper Code Coverage file for the testcase in the local machine.

But if I use nginx or pm2 server, how can I do the same?

As I need this to add in the Jekins Pipeline to