javascriptjestjs

How to get the code coverage report using Jest?


Is there a way to have code coverage in the JavaScript Jest testing framework, which is built on top of Jasmine?

The internal framework does not print out the code coverage it gets. I've also tried using Istanbul, blanket, and JSCover, but none of them work.


Solution

  • When using Jest 21.2.1, I can see code coverage at the command line and create a coverage directory by passing --coverage to the Jest script. Below are some examples:

    I tend to install Jest locally, in which case the command might look like this:

    npx jest --coverage

    I assume (though haven't confirmed), that this would also work if I installed Jest globally:

    jest --coverage

    The very sparse docs are here

    When I navigated into the coverage/lcov-report directory I found an index.html file that could be loaded into a browser. It included the information printed at the command line, plus additional information and some graphical output.