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

how to generate code coverage against the source files instead of the bundle file #62

Open
30atm opened this issue Aug 31, 2020 · 11 comments
Labels
question Further information is requested

Comments

@30atm
Copy link

30atm commented Aug 31, 2020

Hi,
Could someone please advise if code coverage can be generated for reactjs source code instead of the bundle generated by babel.

I was wondering if source maps can be used to generate code coverage

thanks

@bcoe bcoe added the question Further information is requested label Sep 11, 2020
@anirudh-modi
Copy link

@emelens were you able to find any solution for this by any chance?

@nmfzone
Copy link

nmfzone commented Mar 8, 2021

Bump this issue

@dword-design
Copy link

@emelens This could maybe solve the issue #64. Or this #61.

@278kunal
Copy link

This is much needed can anyone please share your thoughts here ? @bcoe

@dword-design When do you plan to merge your above changes which can enable source map support here ?

@dword-design
Copy link

@278kunal Not sure yet. The solution in the PR works for me but don't know if it works for others. What do you or others say?

@278kunal
Copy link

@dword-design I cloned your fork and tried getting coverage via Puppeteer of a locally running React application bundled via webpack. But unfortunately I am only seeing coverage for the webpack transpiled code. Is there a way we can tell PTI to look for source maps when passing to v8-to-istanbul ?

@dword-design
Copy link

@278kunal Ah yeah I guess it's because the PR only extracts source maps from webpack bundled code. I extract them from the base64. Where else could PTI find source maps?

@danvk
Copy link

danvk commented May 7, 2022

Two suggestions here if you're using webpack:

  • If you set devtool: 'eval-source-map' (or anything with "eval" in it) in your webpack.config.js, then you'll also get coverage data for something close to your original source files out of puppeteer.
  • Instead of collecting coverage via puppeteer's page.coverage.startJSCoverage(), you can instrument your code using coverage-istanbul-loader, run it with puppeteer and then extract the coverage data (already in Istanbul format) using const coverage = await page.evaluate('window.__coverage__');

If the second option is a possibility, it works very nicely!

@a4vg
Copy link

a4vg commented Oct 23, 2022

@danvk Sorry if this is more of an Istanbul related question, I'm just starting to use it.

For the second option, after getting the coverage (from window.__coverage__) should I just dump it into a JSON file, put it into the .nyc_output folder and run nyc report --reporter=html to get Istanbul's report?

@rschoenbichler
Copy link

rschoenbichler commented Nov 30, 2022

@a4vg could you please share some insights on how you've generated the istanbul report from the coverage once retrieved from within the page?

@danvk it seems like this solution would not allow to collect coverage data across multiple page reloads (like with page.coverage.startJSCoverage({ resetOnNavigation: false })) ?

@danvk
Copy link

danvk commented Mar 26, 2023

@a4vg yes, put that object in a JSON file in .nyc_output. @rschoenbichler I wound up with a separate JSON coverage file for each test case, which I then merged with nyc merge. I'm not sure whether this would still work if your tests involve navigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

9 participants