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.
This question is related to
javascript
jestjs
Check the latest Jest (v 0.22): https://github.com/facebook/jest
The Facebook team adds the Istanbul code coverage output as part of the coverage report and you can use it directly.
After executing Jest, you can get a coverage report in the console and under the root folder set by Jest, you will find the coverage report in JSON and HTML format.
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.
If you are having trouble with --coverage not working it may also be due to having coverageReporters enabled without 'text' or 'text-summary' being added. From the docs: "Note: Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output." Source
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 a coverage folder in the root directory. /coverage/icov-report/index.html has the HTML view of the code coverage.
UPDATE: 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
"devDependencies": {
"jest-cli": "git://github.com/hankhsiao/jest.git"
}
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).
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"],
}
See Jest documentation for coverageReporters (default is ["json", "lcov", "text"]
)
Or add --coverage
when you invoke jest.
If you are using the NestJS framework, you can get code coverage using this command:
npm run test:cov
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:
You must put --
before passing the --coverage
argument of Jest
npm test -- --coverage
if you try invoking the --coverage
directly without the --
it won't work
You can pass the --coverage
argument of jest directly
yarn test --coverage
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: Code Coverage with Chutzpah
This works for me:
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"]
},
"scripts": {
"test": "jest --coverage"
},
Run:
yarn/npm test
Source: Stackoverflow.com