Search code examples
reactjscypressistanbulnyc

How to exclude files from Istanbul nyc coverage report


I am just trying to get the basic Istanbul nyc exclude functionality working on my project but so far I cannot.

I am using Cypress to run e2e test on my react.js application. My coverage report seems to be working fine other than it not ignoring the files in my exclude array.

I have tried adding this to my package.json file:

"nyc": {
        "exclude": [
            "AddressFormatter.js"
        ]
    }

I have tried including the full path name src/components/AddressFormatter as well as **/AddressFormatter.js.

I have also tried using a .nycrc.json with the following content:

{
    "reporter": ["lcov", "text-summary"],
    "sourceMap": true,
    "instrument": true,
    "temp-dir": "app/test/.nyc_output",
    "report-dir": "app/test/coverage",
    "all": true,
    "exclude": ["**/AddressFormatter.js"],
    "check-coverage": true
}

The steps I took to find this problem. From the terminal I ran

yarn add -D @cypress/code-coverage

Then I ran

yarn add -D nyc

Next I ran

nyc instrument ./src ./coverage

Lastly I updated the package.json start script with

"start": "react-scripts -r @cypress/instrument-cra start"

After this I tried all of the methods above to get my coverage report to ignore different files.

So far I have had no success ignoring this file. Any suggestions would be appreciated.


Solution

  • Following the docs seems that there are a couple of options you can try:

    • Add the exclude option into your code coverage environment variable:
    // cypress.config.js or cypress.json
    env: {
      codeCoverage: {
        exclude: ['**/AddressFormatter.*'],
      },
    },
    
    • Use the excludeAfterRemap option. Quoting the repo README:

    Another important option is excludeAfterRemap. By default it is false, which might let excluded files through. If you are excluding the files, and the instrumenter does not respect the nyc.exclude setting, then add excludeAfterRemap: true to tell nyc report to exclude files.

    • Lastly there is an example repo you can compare yours with