Search code examples
reactjsmocha.jsistanbul

Istanbul Code Coverage


I have a React project with tests written in Mocha. Istanbul is used to get the test coverage.

I have 4 react components in a folder called lib and in the test folder, I have only tests written for 2 of the components. When Istanbul reports the coverage, it says 100% but clearly it's not 100% for the project because there are components without tests. How to get the correct coverage report for the components in lib folder?

My .istanbul.yml looks like this

instrumentation:
  root: ./lib
  extensions: ['.js', '.jsx']
reporting:
    print: summary
    reports:
        - lcov
    dir: ./coverag

And the script in package.json looks like this

"code_cov": "babel-node ./node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- ./test/**/*.spec.js"

and the folder structure looks like this

/
  /lib
   - Component1.jsx
   - Component2.jsx
   - Component3.jsx
   - Component4.jsx
  /test
   - Component1.spec.js
   - Component2.spec.js
   - Component3.spec.js
   - Component4.spec.js
.istanbul.yml
package.json

What am I doing wrong here ? Any guidance would be helpful. Thanks.


Solution

  • Potentially a duplicate.

    The solution is using include-all-sources flag added here.

    In your case it should be:

    "code_cov": "babel-node ./node_modules/istanbul/lib/cli.js --include-all-sources cover node_modules/mocha/bin/_mocha -- ./test/**/*.spec.js"