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.

Document how to use with puppeteer?

See original GitHub issue
  • 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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
queengooborgcommented, Feb 7, 2022

I was able to get puppeteer working with c8, thanks to https://github.com/puppeteer/puppeteer/pull/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]})
  )
);
3reactions
bcoecommented, Feb 21, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer | Puppeteer
Puppeteer is a Node.js library which provides a high-level API to control Chrome/Chromium over the DevTools Protocol. Puppeteer runs in headless mode by...
Read more >
Puppeteer documentation - DevDocs
Puppeteer 7.1.0 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >
Headless Chrome ( Puppeteer ) - how to get access to ...
Getting an ElementHandle for the document body by calling the page.$ method. Adding a class to the body by calling its classList.add method...
Read more >
Web Scraping with a Headless Browser: A Puppeteer Tutorial
In this article, Toptal Freelance JavaScript Developer Nick Chikovani shows how easy it is to perform web scraping using a headless browser.
Read more >
Get a DOM element using Puppeteer - Hello World Master
Learn how to get information from DOM elements retrieved from using Puppeteer, Google's open source browser automation tool.
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