To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.
Syntax. ng test command allows to check code coverage using the test cases written.
As part of the output Jest generates, you'll see not only the test results but also the code coverage report. Since the current code is very simple, you should be getting 100 percent code coverage.
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.
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.
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
This works for me:
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"]
},
"scripts": {
"test": "jest --coverage"
},
Run:
yarn/npm test
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.
You can run npx jest --coverage -- path/to/your/file.spec.js that will show coverage for affected files
If you want to view this in browser you can do as follows,
Then you can visually see all the coverage areas.
You can also refer to this link below, for more information https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With