istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.55k stars 353 forks source link

Unable to exclude files from my coverage report #1476

Open tdammonsandboxx opened 2 years ago

tdammonsandboxx commented 2 years ago

I am just trying to get the basic 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.

jbpallingayan commented 1 year ago

having the same issue.

sebastianwessel commented 1 year ago

I had similar issues.

See official documentation:

nyc will instrument all files if the --all flag is set or if running nyc instrument. In this case all files will appear in the coverage report and contribute to coverage statistics.

Try to remove "all": true, from your .nycrc.json and run without instrument parameter

For me, it helped to explicit add the include/exclude (in addition to the config file) to command like: nyc --include=lib/* --exclude=**/to_exclude_folder/**

In general, there seems to be an issue, if you use command line flags and a config file. Use config file OR command line flags. During debug, I was mixing it up and got strange behavior.