question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when merging Jest coverage using nyc

See original GitHub issue

šŸ› 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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

1reaction
zaunicommented, 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$)']
1reaction
postmodernistxcommented, 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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving ā€œInvalid file coverage object, missing keys, found ...
If you are merging two NYC coverage reports, for example your Cypress and your Jest coverage reports, you may encounter the cryptic error...
Read more >
Jest: How to merge coverage reports from different jest test runs
I have tried using nyc to combine coverage-final*.json files from tmp folder and output to a full-test-coverage/ folder.
Read more >
Triple combined code coverage for React Apps with Jest ...
Nyc has a utility to specify the folder location for the reports to be merged. Our coverage files are under reports folder. After...
Read more >
@storybook/test-runner - npm
After merging test coverage reports coming from the test runner with reports from other tools (e.g. Jest), if the end result is not...
Read more >
Generating Code Coverage Report for Cypress - Marie Cruz
nyc_output folders and copies the respective json files from cypress-coverage and jest-coverage to the coverage folder. combine:reports - using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found