-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Feature] Track Code Coverage via istanbul #7030
Comments
We currently use jest-playwrights' code coverage feature and we depend on it. We would love to migrate to new playwright test-runner but we have to have the code coverage feature, and would really hope it would be integrated. Keep up the amazing work guys! 🚀 🥇 |
Thanks, i need coverage with istanbul. stay tuned |
Small update, we created an example guide how you can use it. It is currently there to get an understanding which additional features are necessary to provide a full story around code coverage collections. For any suggestions / feedback it would be highly appreciated to get input especially:
|
For me:
|
I implemented the baseFixtures.ts in the test folder and I created my test like this:
|
How did you add the babel-plugin? This requires a transcompilation step which is not out-of-the box available. This looks like create-react-app and needs by that an ejected installation. |
I only created the .babelrc file in the root project. That's means that I can't apply coverage if I use creat-react-app or ng new my-app? |
create-react-app does not use the babelrc file. For that you would need to either eject your create-react-app or use react-app-rewrired |
Ok thanks Max, for an angular app is the same case or I can create a babelrc file? |
I'm not familiar with Angular, depends how/if they allow you to add a custom babel plugin. |
hi, this is only a typings warning. It should still be able to work correctly. To get rid of the typing warnings, replacing it with import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto'; should fix. Would be awesome if you could verify, then I can adjust it properly. |
According to my tests Istanbul coverage and v8 coverage cannot be merged together. I have opened an issue to track this: istanbuljs/v8-to-istanbul#144. Maybe there is a way but I couldn't make it work. If confirmed it means coverage collected using Istanbul cannot be merged with coverage returned by page.coverage.stopJSCoverage(). Dropping this here in case you was not aware of this issue. |
Closing since its possible in the user-land, see https://github.com/mxschmitt/playwright-test-coverage |
@mxschmitt It would be great if this were offered as part of Playwright itself via an option instead of in user-land. Thoughts? |
@mxschmitt Do you have |
Does anyone collect the coverage of playwright component testing with It doesn't collect the coverage in my local using I'm not sure but the following code should be updated because |
Sorry, the above comment was my failure 🤦♂️ |
Hey @dhythm, I'm trying to get the same combination working, but it doesn't even work with normal playwright. I also have In my
I have the same code as your screenshot, where did you put it? While I'm figuring this out, I'm running my tests using Anything I'm missing here? EDIT: I fixed NORMAL playwright by changing the command I'm running to However, when I run the same, but with
|
is there an option in playwright config to turn this on? |
@dhythm, I am in the same position you were. I have my normal Playwright tests working with coverage using |
@bradyisom do you have an example of how you were able to get normal playwright tests working with I'm not sure why this isn't a feature within playwright to check for instrumented code. Other integration packages like |
@prests, I don't have a simple repro, but there were a few key pieces for me to get it working:
|
Hey @dhythm, will you be able to share an example repo with your changes, we are struggling to make this work |
Just integrate the code coverage report into monocart-reporter
If you are interested, try it according to the examples: |
TLDR: see this comment for detailed guidance and code demo on setting up code coverage. @mxschmitt This issue was opened a couple of years ago and I was wondering if you know what the plans are for this? Meaning, do you plan to provide a better "out of the box" experience to get code coverage? Thank you for your guide to get code coverage with istanbul. Thanks to it I've been able to get code coverage working in my angular app which is started by playwright's However, my journey to get code coverage working in Playwright was very painful. I suspect I'm not alone on this. I think a better out of the box experience or at least better docs outlining different ways to get code coverage to work would be a welcome improvement. I believe @anishkny is right when he says this would be better if it wasn't something only in user-land. 🙏 My Playwright code coverage journeyI started by looking at the official docs on class-coverage and I couldn't get the example in the docs to work for my scenario. My test was just this: test('basic test', async ({ page }) => {
if ([test.info](http://test.info/)().project.name === 'chromium') {
await page.coverage.startJSCoverage();
}
await page.goto('/');
if ([test.info](http://test.info/)().project.name === 'chromium') {
const jsCoverage = await page.coverage.stopJSCoverage();
for (const entry of jsCoverage) {
const converter = v8toIstanbul('', 0, {
source: entry.source,
});
await converter.load();
converter.applyCoverage(entry.functions);
const coverageJSON = JSON.stringify(converter.toIstanbul());
}
}
}); And my use: {
baseURL: "http://127.0.0.1:4999"
},
webServer: {
command: "npx ng serve --port 4999",
url: "http://127.0.0.1:4999",
reuseExistingServer: !_env.CI
} In terms of the test itself, all is fine, playwright's
@cenfun was kind to help me on Playwright's discord channel by answering on my help post Help with code coverage example from the docs. He pointed out that the problem was that At this point I decided to pivot, I'm not an experienced frontend developer and I didn't feel comfortable trying to do what @cenfun suggested to get the I then found this issue and adapted your guide to my Angular project. This again wasn't a straightforward thing for me to do. I had to learn a bit about how Angular doesn't expose it's At this point I was happy because I got code coverage to work. However, it felt that I had to do too much:
The above felt a bit much and it also felt like something I'd have to document well on my project's README because my team/other people in the company would not easily grasp all the moving parts. Feeling that the above was too much I decided to pivot again and try @cenfun 's monocart-reporter. I eventually found Playwright Component Testing Vue Example and following the instructions on that repo I was able to get monocart-reporter to produce code coverage for my app. Unfortunately the code coverage from The end of my journeyThe whole journey took about Now I understand much more about code coverage in general in javascript projects, not just regarding playwright and if I had a lot of this base code coverage knowledge when I started the journey I feel like I could have got things to work much quicker. However, the point still remains that, at least for developers less experienced with frontend apps, it's way more difficult to get code coverage working than it should be and requires learning/knowing about several concepts. Furthermore, the example in the docs just didn't work at all for my scenario which used the Related issues and links
|
@edumserrano can you share your webpack config to get babel to work with Angular? |
@edumserrano Thanks! I asked because we have been using @jsdevtools/coverage-istanbul-loader through a Webpack configuration. However, I recently noticed that it messes up sourcemaps. The breakpoints are off and some valid lines you can't debug. I want to try to babel-plugin-istanbul to see if it works better with sourcemaps. |
To reiterate on @cenfun's comment, you can use the monocart-reporter to get code coverage in istanbul without having to instrument your code with babel-plugin-istanbul. You can follow Playwright's documentation on code coverage to collect code coverage in V8 and and then use the monocart-reporter to convert the V8 code coverage to istanbul via the the toIstanbul code coverage option. I've had issues with incorrect code coverage on my project using the v8-to-istanbul library shown on Playwright's documentation. See istanbuljs/v8-to-istanbul#216. @cenfun fixed the V8 to istanbul conversion issue on monocart-reporter by removing the dependency on the v8-to-istanbul and writing something to replace it. See cenfun/monocart-reporter#50. Since the fix, I've had no issues with incorrect code coverage using it. UPDATE: |
As an update, the situation here has gotten much less dire in the months since @edumserrano's June 4 comment, thanks to the excellent work by @cenfun on monocart-reporter and monocart-coverage-reports. I seriously cannot thank you enough for your valiant efforts there! For those of you using Next.js, I made an example project, which should help serve as a starting point. |
Indeed @cenfun's effort with monocart-reporter has been phenomenal. With the recently release of Like @michaelhays, I have recently created a set of sample projects on the edumserrano/playwright-adventures repo that should help people take advantage of the
Hopefully these demos will provide enough guidance for people to get code coverage for their projects. |
@cenfun, @michaelhays, monocarg-reporter looks very interesting, I am going to try it. I have one question: does it support server code coverage? We are using NextJS and have the server side code using express |
^ Server-side coverage is being tracked at cenfun/monocart-coverage-reports#1, for those interested |
Update: It can be easily integrated in the user-land now.
For that follow the guide here.
For any suggestions please comment inside this issue.
The text was updated successfully, but these errors were encountered: