Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when merging Jest coverage using nyc #9808

Closed
stahloss opened this issue Apr 13, 2020 · 10 comments · Fixed by #10631
Closed

Error when merging Jest coverage using nyc #9808

stahloss opened this issue Apr 13, 2020 · 10 comments · Fixed by #10631

Comments

@stahloss
Copy link

stahloss commented Apr 13, 2020

🐛 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
@tobiasgrossmann
Copy link

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
Copy link

Ok - using this workaround: #2418

@jeysal
Copy link
Contributor

jeysal commented Oct 13, 2020

accidentally closed

@stahloss
Copy link
Author

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)

@TristanHessellHyper
Copy link

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

@postmodernistx
Copy link

postmodernistx commented Feb 18, 2022

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

@zauni
Copy link

zauni commented Jul 28, 2022

@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$)']

@github-actions
Copy link

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 github-actions bot added the Stale label Jul 28, 2023
@github-actions
Copy link

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 github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants