How to enable Istanbul code coverage for the mocha plugin?
See original GitHub issueWhat problem does this feature solve?
Coverage reporting for unit tests
What does the proposed API look like?
Is there a way to enable coverage for the mocha plugin? When set up manually on another project, you add the istanbul coverage instrumenter as a webpack loader like this:
webpackConfig.module.rules.unshift({
test: /\.(js)$/,
loader: `istanbul-instrumenter-loader`,
options: {esModules:true},
exclude: [
path.resolve(`node_modules`),
// Exclude tests from test coverage
path.resolve(`src/tests`),
/\.(spec|e2e|tests)\.ts$/,
]
})
And then you need to add lcov
as a reporter.
This explanation is pretty comprehensive: https://github.com/zinserjan/mocha-webpack/blob/master/docs/guides/code-coverage.md
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:48 (3 by maintainers)
Top Results From Across the Web
Code coverage reporting in Javascript with Istanbul in Mocha
First of all, we will add some pre-requisite items like a JS file which we are testing and then add unit test for...
Read more >Mocha Code Coverage: How to Use Istanbul to Get Going
Istanbul is a test coverage tool that works with many different frameworks. It tracks which parts of your code are executed by your...
Read more >Integrate Istanbul for test coverage with Mocha
I have a script that runs tests using the Mocha framework. Because I use Babel, I need to require the plugin in the...
Read more >How to implement Istanbul Coverage with Selenium and Mocha
Getting istanbul working. I had the same problem you had months ago. All zeroes in the reports. The problem has to do with...
Read more >Using Istanbul With Mocha
Istanbul is extensively tested with mocha, which we use for many of our own repos. ... that's it! this will instrument the code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s how I made test coverage work for both
apollo-server
andsrc
directory:UPDATE: This comment is outdated, please check out @stephsavoie’s solution below.
[1/4] Install
babel-plugin-istanbul
+ configure it inbabel.config.js
[2/4] Install
istanbul-instrumenter-loader
+ configure it invue.config.js
[3/4]: Install
nyc
+ configure it inpackage.json
[4/4]: Use it
nyc vue-cli-service test:unit
nodemon --exec nyc vue-cli-service test:unit
PS: I also recommend adding
coverage
and.nyc_output
to your.gitignore
.Based on @caugner 's answer, I made it work in a simpler way (by skipping the step 2 and enhanced a bit the nyc configuration in package.json) I’m using Vue CLI-3 with Mocha-webpack as my tests runner.
Here’s my current configuration:
[1/3] Install babel-plugin-istanbul + configure it in babel.config.js
[2/3]: Install
nyc
+ configure it in nyc.config.js[3/3] Use it
No more errors or anything else. I’m not sure if I am missing something but at first sight, coverage report seems to represent the reality of tests scenarios I’ve created so far.
EDIT: Updated configuration to fix wrong .vue file paths in the coverage report and enhance it to my personal taste