Code coverage for Jest

Alex Palcuie picture Alex Palcuie · Jul 18, 2014 · Viewed 165.7k times · Source

Is there a way to have code coverage in the Javascript Jest testing framework that 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.

Answer

ccalvert picture ccalvert · Oct 24, 2017

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.