istanbuljs / nyc

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

Combined coverage for Branches does not add up to 100% #1508

Open samhatoum opened 1 year ago

samhatoum commented 1 year ago

EDIT: this issue as partly fixed by using babel instead of v8 in Jest, however the branches still do not add up to 100% despite the single report from Jest does show 100% branch coverage. More in the thread.

Link to bug demonstration repository

https://github.com/samhatoum/monorepro/tree/nyc (note the nyc branch`)

Expected Behavior

Combined coverage from two test runs should add up 100% when one of the input coverage reports has 100% coverage

Observed Behavior

Combined coverage is reduced when the second test run contains less than 100% test coverage

Here's the report from Jest

jest 100-100-100-100

Here's the report from Cucumber:

cucumber 60-50-40-55

Here's the combined report:

combined 88-83-66-100

.nycrc.json

{
  "require": ["tsconfig-paths/register"],
  "extends": "./nyc-config.js",
  "cwd": ".",
  "all": true,
  "reporter": [
    "lcovonly",
    "json",
    "html"
  ],
  "temp-dir" : "coverage/.nyc_output",
  "include": ["**/*.{ts,tsx}"],
  "exclude": [
    "jest.config.ts",
    "specs/*"
  ]
}

package.json scripts that produce the above

    "test": "yarn ensure:dirs && yarn test:jest && yarn test:cucumber && yarn coverage:combine",
    "test:jest": "jest --coverage",
    "test:cucumber": "nyc --report-dir=./coverage/cucumber cucumber-js",
    "ensure:dirs" : "(rm -rf coverage || true) && (mkdir -p coverage/reports || true)",
    "ensure:dir:nyc" : "(rm -rf ./coverage/.nyc_output || true) && mkdir ./coverage/.nyc_output",
    "coverage:copy:reports:cucumber": "cp coverage/cucumber/coverage-final.json coverage/reports/from-cucumber.json",
    "coverage:copy:reports:jest": "cp coverage/jest/coverage-final.json coverage/reports/from-jest.json",
    "coverage:copy:reports": "yarn coverage:copy:reports:jest && yarn coverage:copy:reports:cucumber",
    "coverage:combine:reports": "yarn ensure:dir:nyc && nyc merge coverage/reports && mv coverage.json coverage/.nyc_output/out.json",
    "coverage:finalize:combined-report": "nyc report --reporter html --reporter text-summary  --report-dir coverage/combined-coverage --cwd .",
    "coverage:combine": "yarn coverage:copy:reports && yarn coverage:combine:reports && yarn coverage:finalize:combined-report"

Troubleshooting steps

Environment Information

  System:
    OS: macOS 13.2
    CPU: (12) arm64 Apple M2 Max
    Memory: 17.21 GB / 96.00 GB
  Binaries:
    Node: 18.8.0 - ~/.nvm/versions/node/v18.8.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.8.0/bin/yarn
    npm: 9.1.2 - ~/.nvm/versions/node/v18.8.0/bin/npm
  npmPackages:
    nyc: ^15.1.0 => 15.1.0 
    ts-node: ^10.9.1 => 10.9.1 
    typescript: ~4.9.4 => 4.9.4 
AriPerkkio commented 1 year ago

You can't mix Istanbul instrumented coverage with V8 coverage. Use the same instrumenter for both test setups.

E.g. change 'babel' here to use istanbul with Jest: https://github.com/samhatoum/monorepro/blob/c6203edacde44b93eb5a48cf09feaea9b408ade0/jest.config.ts#L6

samhatoum commented 1 year ago

Thank you, that's definitely improved it, but the branches now show as 50% combined coverage.

improved

I've updated the repo

varunrajasekhar commented 11 months ago

@samhatoum - were you able to resolve it ? I am having the same issue with nyc 15.1.0 , Node 18.17.0 and mocha 10.2.0 version

https://github.com/istanbuljs/nyc/issues/1540

samhatoum commented 11 months ago

No, just learned to live with it and reduce the thresholds. I'd love to see it fixed