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

Document how to use with puppeteer? #162

Open
stevenvachon opened this issue Nov 12, 2019 · 12 comments
Open

Document how to use with puppeteer? #162

stevenvachon opened this issue Nov 12, 2019 · 12 comments
Labels

Comments

@stevenvachon
Copy link

  • Version: 12.13.0
  • Platform: Darwin Stevens-Mac-mini.local 19.0.0 Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64 x86_64

Previously, with nyc, I was using puppeteer-to-istanbul, but now I'm not sure what to do. c8 needs to receive the reports from puppeteer somehow.

@SergeyPirogov
Copy link

Puppeteer aggregates results that is returned from raw chrome dev tools protocol

@bcoe bcoe added the question label Nov 28, 2019
@bcoe
Copy link
Owner

bcoe commented Nov 28, 2019

@stevenvachon, c8 and nyc both just process Istanbul format reports, and output coverage based on them. It's probably worth sticking with nyc 👍

@bcoe bcoe closed this as completed Nov 28, 2019
@stevenvachon
Copy link
Author

@bcoe then why are you creating this library, if not to replace nyc and/or the use of istanbul?

@bcoe
Copy link
Owner

bcoe commented Nov 30, 2019

then why are you creating this library, if not to replace nyc and/or the use of istanbul?

Because they do two different things? Istanbul transpiles your source code into a form that collects test coverage, c8 turns on V8's built in test coverage and runs reports.

It happens that puppeteer can be made to output coverage in the same form as V8, so some of the toolchain for processing these reports is shared.

@bricss
Copy link

bricss commented Nov 1, 2020

Ahoy ahoy 🦜

If anyone would like to know how to use c8 to get a coverage report with mocha + playwright 👀
You can find it here -> fixtures.mjs 📜

@queengooborg
Copy link

I was able to get puppeteer working with c8, thanks to puppeteer/puppeteer#6454! I wanted to leave the code I used here for anyone else who may want to stick with puppeteer. The trick is to set the includeRawScriptCoverage to true in your startJSCoverage() options, and then write its output to JSON files. Huge thanks to @bricss for providing a foundation for me to work off of!

The code I wrote is as follows:

await page.coverage.startJSCoverage({includeRawScriptCoverage: true});

// Do your tests...

const jsCoverage = await page.coverage.stopJSCoverage();

// Point to original .js files
const coverage = jsCoverage
  .map(({rawScriptCoverage: it}) => ({
    ...it,
    scriptId: String(it.scriptId),
    url: it.url.replace(
      new RegExp(`${ origin }(?<pathname>.*)`),
      (...[, , , , { pathname }]) => `${
        pathname.match(/^\/src/)
        ? process.cwd()
        : path.resolve(dirPath, base)
      }${ pathname.replace(/([#?].*)/, '')
                  .replace(/\//g, path.sep) }`,
  })));

// Export coverage data
jsCoverage.forEach((it, idx) =>
  fs.writeFileSync(
    `${
      process.env.NODE_V8_COVERAGE
    }/coverage-${Date.now()}-${idx}.json`,
    JSON.stringify({result: [it]})
  )
);

@bcoe bcoe added docs and removed question labels Feb 21, 2022
@bcoe bcoe changed the title How to use with puppeteer? Document how to use with puppeteer? Feb 21, 2022
@bcoe bcoe reopened this Feb 21, 2022
@bcoe
Copy link
Owner

bcoe commented Feb 21, 2022

@queengooborg this is slick, we should document how to do this 😄

@queengooborg
Copy link

That would be great, yeah! I was thinking of submitting a PR to add it, but I wasn't sure where to add it -- any particular location you'd think would work best?

@bcoe
Copy link
Owner

bcoe commented Feb 21, 2022

@queengooborg we could add a docs/puppeteer.md, and then add a ## Guides section in the top level README perhaps?

@bricss
Copy link

bricss commented Feb 21, 2022

IMO, it might be both docs/playwright.md and docs/puppeteer.md 🙂 or even docs/chromium.md, bcos the code for both playwright and puppeteer would be same same, with only one difference in includeRawScriptCoverage: true passed in startJSCoverage() method, as we already know 🤓

@a4vg
Copy link

a4vg commented Nov 11, 2022

Any updates on this?

@testgitdl
Copy link

startJSCOverage does not seem to have this includeRawScriptCoverage: true

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

No branches or pull requests

7 participants