c8 – native V8 code-coverage

c8 node <script_name>:

Code coverage provides information about whether, and optionally how often certain parts of an application have been executed. It’s commonly used to determine how thoroughly a test suite exercises a particular codebase.

we had detailed V8 coverage information being output, but no easy way to output human readable reports. Two npm modules were written to facilitate this:

  • v8-to-istanbul, which converts from V8 format coverage output to Istanbul format.
  • c8, which pulls together the entire inspector dance into a single command, sot you can collect coverage by simply running c8 node foo.js.
npm i c8 -g
c8 node foo.js

The above example will output coverage metrics for foo.js. It will create a folder named coverage containing a temp folder that has a JSON file.

         |_ temp 
                | _ *.json

supplying –all will cause c8 to consider all src files in the current working directory when determining coverage.

c8 --all node foo.js

supplying –clean should temp files be deleted before script execution.

c8 --clean --all node foo.js
c8 --clean node foo.js

We can use some other flags like –include or –exclude to include or exclude the files respectively.

c8 Report:

run c8 report to regenerate reports after c8 has already been run. We can check the report of the existing coverage JSON file. c8 report always looks for a coverage folder in which there is a temp folder containing coverage JSON file.

We can generate the html report of the coverage using report command like below

c8 report -r html

You can read Rethinking JavaScript Test Coverage written by Benjamin Coe, Product Manager at npm, Inc. and lead maintainer of yargs and Istanbul. To know how c8 comes into picture when Istanbul already exist.


Leave a Reply