ljharb / istanbul-merge

Merge multiple istanbul coverage reports into one.
MIT License
53 stars 7 forks source link

After merge, unexpected `branch not covered` at trailing char `{` #43

Open loynoir opened 1 year ago

loynoir commented 1 year ago

Description

Run reproduce.mjs with env RUNTIME=node and RUNTIME=browser.

Merge coverage-final.json from different runtime.

Expect 100% coverage.

Actually, branch not covered at trailing char {

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |     100 |    33.33 |     100 |     100 |                   
 reproduce.mjs |     100 |    33.33 |     100 |     100 | 4-8               
---------------|---------|----------|---------|---------|-------------------
reproduce.mjs:1:const IS_NODE = process.env['RUNTIME'].includes('node')
reproduce.mjs:2:const IS_BROWSER = process.env['RUNTIME'].includes('browser')
reproduce.mjs:3:
reproduce.mjs:4:if (IS_NODE) {
reproduce.mjs:5:  console.log('node')
reproduce.mjs:6:}
reproduce.mjs:7:
reproduce.mjs:8:if (IS_BROWSER) {
reproduce.mjs:9:  console.log('browser')
reproduce.mjs:10:}
reproduce.mjs:11:
reproduce.mjs:12:export { }

Reproduce Repo

https://github.com/loynoir/reproduce-istanbul-lib-coverage-737

Related https://github.com/istanbuljs/istanbuljs/issues/737

https://playwright.dev/docs/api/class-coverage

ljharb commented 1 year ago

How exactly do i run reproduce.js so that it produces coverage data?

loynoir commented 1 year ago

@ljharb

Below script simulate collect coverage from different runtime.

https://github.com/loynoir/reproduce-istanbul-lib-coverage-737/blob/main/test.sh

And c8 is a wrapper for NODE_V8_COVERAGE=dir

ljharb commented 1 year ago

On node v20.5.0, everything seems to work just fine, including the nyc command. What version of node are you using?

loynoir commented 1 year ago
$ git clean -dfx > /dev/null
$
$ node --version
v20.4.0
$ npm i && bash ./test.sh

added 261 packages, and audited 262 packages in 5s

70 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
+ env RUNTIME=node,browser npm exec -- c8 --report-dir build/expected node ./reproduce.mjs
node
browser
---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |     100 |      100 |     100 |     100 |                   
 reproduce.mjs |     100 |      100 |     100 |     100 |                   
---------------|---------|----------|---------|---------|-------------------
+ env RUNTIME=node npm exec -- c8 --report-dir build/actual/node node ./reproduce.mjs
node
---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   83.33 |       50 |     100 |   83.33 |                   
 reproduce.mjs |   83.33 |       50 |     100 |   83.33 | 9-10              
---------------|---------|----------|---------|---------|-------------------
+ env RUNTIME=browser npm exec -- c8 --report-dir build/actual/browser node ./reproduce.mjs
browser
---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   83.33 |       50 |     100 |   83.33 |                   
 reproduce.mjs |   83.33 |       50 |     100 |   83.33 | 5-6               
---------------|---------|----------|---------|---------|-------------------
+ mkdir -p ./build/actual/temp
+ npm exec -- istanbul-merge --out ./build/actual/temp/coverage.json ./build/actual/node/coverage-final.json ./build/actual/browser/coverage-final.json
+ npm exec -- nyc report -r lcov -r text --temp-directory ./build/actual/temp --report-dir ./build/actual
---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |     100 |    33.33 |     100 |     100 |                   
 reproduce.mjs |     100 |    33.33 |     100 |     100 | 4-8               
---------------|---------|----------|---------|---------|-------------------
$
ljharb commented 1 year ago

ohhh right sorry, it's not that it errors, it's that there's 4 uncovered lines :eyes:

ljharb commented 1 year ago

All this package is doing is wrapping istanbul's istanbul.createCoverageMap, and using .merge on it. I think this is a bug in istanbul.