jest icon indicating copy to clipboard operation
jest copied to clipboard

Error when merging Jest coverage using nyc

Open stahloss opened this issue 4 years ago • 7 comments

🐛 Bug Report

I'm using nrwl's nx with angular, jest and cypress. For unit test code coverage reports I execute npm run test -- --code-coverage and the report looks good when opening. Now I'd like to use nyc to merge this report with the one generated from cypress. Running nyc merge runs fine on the cypress coverage json, but on Jest's I get Invalid file coverage object, missing keys, found:data When I look in the coverage-final.json file from Jest, I do see html objects containing data properties, while the .ts objects have keys properties. The cypress coverage-final.json output has html objects containing keys properties, which leads me to believe the problem is with the way Jest outputs coverage under certain circumstances. Possibly it's an Istanbul issue, but I'm posting it here as it seems Istanbul's nyc, that uses the same version of Istanbul as Jest is not able to process the coverage files.

Also worth mentioning, with Jest 24 the coverage-final.json didn't contain data properties in html entries.

To Reproduce

Not able to reproduce using a minimal setup.

Expected behavior

No data properties to be present on file objects in coverage-final.json.

Link to repl or repo (highly encouraged)

I created a small setup but there the data properties on the .html files somehow were not present. So at the moment I am unable to reproduce outside of the environment this is happening.

envinfo

  System:
    OS: Windows 10 10.0.18363
    CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  Binaries:
    Node: 13.6.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.0 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 25.2.4 => 25.2.4

stahloss avatar Apr 13 '20 16:04 stahloss

Is there any known workaround?

Example Result from jest ("jest": "^26.4.2"): {"C:\\service\\config.js": {"data":{"path":"C:\\service\\config.js",

Example Result from Cypress/NYC: {"C:\\client\\ClientApp\\src\\App.js":{"path":"C:\\client\\ClientApp\\src\\App.js",

tobiasgrossmann avatar Sep 30 '20 11:09 tobiasgrossmann

Ok - using this workaround: https://github.com/facebook/jest/issues/2418

tobiasgrossmann avatar Sep 30 '20 12:09 tobiasgrossmann

accidentally closed

jeysal avatar Oct 13 '20 18:10 jeysal

Ok - using this workaround: #2418

@tobiasgrossmann Thanks for the heads-up about this solution. Unfortunately when generating html reports and viewing them shows stack traces on multiple files. Like this one:

Cannot read property 'start' of undefined
TypeError: Cannot read property 'start' of undefined
    at D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-reports\lib\html\annotator.js:53:31
    at Array.forEach ()
    at annotateStatements (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-reports\lib\html\annotator.js:49:33)
    at annotateSourceCode (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-reports\lib\html\annotator.js:239:9)
    at HtmlReport.onDetail (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-reports\lib\html\index.js:401:33)
    at Visitor. [as onDetail] (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-lib-report\lib\tree.js:34:30)
    at ReportNode.Node.visit (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-lib-report\lib\tree.js:114:17)
    at D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-lib-report\lib\tree.js:118:15
    at Array.forEach ()
    at ReportNode.Node.visit (D:\dev\projects\beheer-web\node_modules\istanbul-api\node_modules\istanbul-lib-report\lib\tree.js:117:24)

stahloss avatar Jan 21 '21 09:01 stahloss

For anyone that gets here and is using typescript, we were getting here when the coverage reports included typing files (filename.d.ts).

After excluding those files from coverage reporting the nyc merge .... command worked

TristanHessellHyper avatar Sep 23 '21 12:09 TristanHessellHyper

If it might help anyone, here's what I ended up doing (probably not a great solution, though 😅 ):

The code coverage report generated by Jest, when utilizing vue-cli-service test:unit added the data key to all files that were (in my case) .png, .stories.js and .(spec|test).(js|ts).

In my jest.config.js I could remove some of those troublemakers by adding the coveragePathIgnorePatterns property:

coveragePathIgnorePatterns: [
  '<rootDir>/src/assets/*',
  '<rootDir>/src/stories/*',
],

But this didn't cover all cases, so I ended up writing a tiny script that stripped the coverage-final.json file of the remaining items with the data property. Some mad regex skills could probably have covered those cases in the coveragePathIgnorePatterns array.

Screenshot example below (coverage-final.json), png files have the data property but regular vue components don't:

image

postmodernistx avatar Feb 18 '22 15:02 postmodernistx

@postmodernistx thanks, with your comment I got it fixed! In my case HTML files were the troublemakers and I could fix it by adding this to jest.config.js:

coveragePathIgnorePatterns: ['/node_modules/', 'src/(?=.*\\.html$)']

zauni avatar Jul 28 '22 12:07 zauni

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 28 '23 12:07 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Aug 27 '23 13:08 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Sep 27 '23 00:09 github-actions[bot]