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

Runtime global __coverage__ object #376

Closed
rohit-gohri opened this issue Mar 4, 2022 · 5 comments
Closed

Runtime global __coverage__ object #376

rohit-gohri opened this issue Mar 4, 2022 · 5 comments
Labels

Comments

@rohit-gohri
Copy link

rohit-gohri commented Mar 4, 2022

  • Version: 16.13.0
  • Platform: Darwin apples-MacBook-Pro.local 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64

Background

I'm currently instrumenting my backend Typescript code using nyc for getting code coverage for our Cypress E2E test suite (docs). And I would like to move to c8.

My current setup involves creating an api route (that is only enabled for NODE_ENV=test) that just returns the global.__coverage__ object so that cypress can collect at the end of the tests, merge with client side coverage and generate a single report.

Problem

AFAIK, c8 doesn't have runtime coverage information available like nyc and instead outputs it as a report. Could we add a flag to enable exposing the coverage at runtime also as a global or a method exposed by the module?


Might be related to #339 but kind of like a reverse approach: we merge c8 coverage with browser collected coverage (using babel-plugin-istanbul).

@bcoe bcoe added the wontfix label Mar 16, 2022
@bcoe
Copy link
Owner

bcoe commented Mar 16, 2022

c8 does not have a global coverage object, it is dumping coverage information that is stored in the V8 JavaScript engine to disk.

One option you might look at is using puppeteer to get the coverage information you are looking for:

puppeteer/puppeteer#6454

@bcoe bcoe closed this as completed Mar 16, 2022
@rohit-gohri
Copy link
Author

rohit-gohri commented Mar 17, 2022

One option you might look at is using puppeteer to get the coverage information you are looking for:

puppeteer/puppeteer#6454

I'm actually looking to get runtime coverage for my backend NodeJS API, and want to query it from a different service.

it is dumping coverage information that is stored in the V8 JavaScript engine to disk.

Is it possible to read it from the running node process (the one being instrumented) and expose it without waiting for the process to exit?

Would something like reading the temp-directory and passing it to https://github.com/istanbuljs/v8-to-istanbul work, or are the reports usable only once the process exits?

@bcoe bcoe reopened this Mar 19, 2022
@bcoe
Copy link
Owner

bcoe commented Mar 19, 2022

Would something like reading the temp-directory and passing it to https://github.com/istanbuljs/v8-to-istanbul work, or are the reports usable only once the process exits?

You can use the inspector protocol in the Node.js process to grab the V8 coverage:

https://github.com/tapjs/processinfo/blob/main/lib/register-coverage.cjs

You can then process this information with c8.

@rohit-gohri
Copy link
Author

I'll try a small PoC and post here how it goes

@rohit-gohri
Copy link
Author

rohit-gohri commented Apr 13, 2022

@bcoe It works pretty well, Thanks! This is the current script I have for reference:
https://gist.github.com/rohit-gohri/5fdef21c9ae60c6941b6bc7493478316

Any comments on the script would be appreciated.

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

2 participants