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.
Try Chutzpah. I have just used it. And I blogged about it on how to integrate in Visual Studio.
This is how I did code coverage with Chutzpah: http://francorobles.wordpress.com/2014/09/14/code-coverage-with-chutzpah/
Jan 2019: Jest version 23.6
For anyone looking into this question recently especially if testing using npm or yarn directly
Currently, you don't have to change the configuration options
As per jest official website, you can do the following to generate coverage reports:
1- For npm:
You must put
--
before passing the --coverage argument of jestif you try invoking the
--coverage
directly without the--
it won't work2- For yarn:
You can pass the
--coverage
argument of jest directlyUPDATE: 7/20/2018 - Added links and updated name for coverageReporters.
UPDATE: 8/14/2017 - This answer is totally outdated. Just look at the Jest docs now. They have official support and documentation about how to do this.
@hankhsiao has got a forked repo where Istanbul is working with Jest. Add this to your dev dependencies
Also make sure coverage is enabled in your package.json jest entry and you can also specify formats you want. (The html is pretty bad ass).
See Jest documentation for coverageReporters (default is
["json", "lcov", "text"]
)Or add
--coverage
when you invoke jest.I had the same issue and i fixed it as below.
npm install --save-dev yarn
npm install --save-dev jest-cli
"jest-coverage": "yarn run jest -- --coverage"
After you write the tests run the command npm run jest-coverage. This will create coverage folder in the root directory. /coverage/icov-report/index.html has the html view of code coverage.
Happy coding!
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.
Alright, ignore my previous answer as someone told me it did not resolve the problem.
New Answer:
1) Check the latest jest (v 0.22): https://github.com/facebook/jest
2) The facebook team puts the istanbul as part of the coverage report and you can use it directly.
3) After executing jest, you can get coverage report on console and under the root folder set by jest, you will find the coverage report in json and html format.
4) FYI, if you install from npm, you might not get the latest version; so try the github first and make sure the coverage is what you need.
Old Answer:
Got the same problem as well. Short answer is:
Istanbul
andJest
are NOT working together.Check the following pages for greater details:
https://github.com/facebook/jest/issues/101
@Ciro Costa: The
config.collectCoverage
is NOT working, as it's a 'TODO' function. Please check the source code.