-
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 #9208
Comments
There's a guide on how to setup test coverage: https://github.com/mxschmitt/playwright-test-coverage Does it help? |
I have tried that out, .nycoutput folder is generated but no reports are generated. |
How do you use this coverage data? (Coverage is typically used in the unit test context only, rarely used for e2e). |
Very similar use case as mentioned here. |
Hi any plans/ETA for this feature request? |
@aslushnikov |
Gentle Reminder!! |
Code coverage of the final bundles would be quite useful to know how much ground the E2E tests cover and to assess whether a new test add sufficient value compared to the cost of running on CI in a big a repo. |
The guide uses |
Playwright actually offers API to get v8's coverage. There is an example using v8-to-istanbul, but it is rather incomplete since it doesn't allow generating coverage report. You can indeed get coverage from v8 and read it from c8. Here is what I did : const { test, expect } = require("@playwright/test");
const fs = require("fs");
const url = require("url");
const path = require("path");
test("test", async ({ page }) => {
// start coverage
await page.coverage.startJSCoverage();
// TODO Your test
...
// Get and save V8 coverage
const coverage = await page.coverage.stopJSCoverage();
const rootPath = path.normalize(`${__dirname}/..`);
const coverageWithPath = coverage.map((entry) => {
const fileName = new url.URL(entry.url).pathname;
return { ...entry, url: `file:///${rootPath}${fileName}` };
});
fs.writeFileSync(
"coverage/tmp/coverage.json",
JSON.stringify({ result: coverageWithPath }, null, 2)
);
}); I had to fix the url, by getting the full path of the files. You might need to adjust After that, just run |
Hi |
@jfgreffier I tried using your code but I do not get any coverage information. I am starting my app via "ng serve -c dev" and afterwards want to get coverage information. hOPE YOU CAN HELP AfterAll(async function () { Output of npx c8 report |
For those who are looking for a temp drop-in solution, I can recommend anishkny/playwright-test-coverage, which is available on NPM. It uses Istanbul instead of C8, which allows to collect coverage from all browsers. Before running tests, the app needs to be built with coverage instrumented (via babel-plugin-istanbul). We use Hope to see built-in coverage reporting at some point! |
@kachkaev thank you very much for checking. I already took this as an example and the problem is that the nyc-output is empty - probably it has to do with the nyc ng serve. Please let me know if you have more ideas - would help big time And the output of npm run test
1: starting server using command "npm run serve"
✔ Browser application bundle generation complete. Initial Chunk Files | Names | Raw Size
Build at: 2022-11-18T14:33:43.337Z - Hash: 0380aa086066a4e4 - Time: 5119ms ** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** √ Compiled successfully.
Running 3 tests using 3 workers 3 passed (7s) To open last HTML report run: npx playwright show-report |
@kachkaev I have created a sample project here: https://github.com/testgitdl/playwright-coverage |
@testgitdl for c8 coverage, yoiu should open the coverage.json and check it. You might have coverage info, or not. Maybe you have coverage info but the wrong path... You should come over to the Slack, a GitHub issue isn't very convenient |
@jfgreffier provided a great example in #9208 (comment) I've improved on his code snippet by putting it into a fixture. // fixtures.ts
import { test as base } from "@playwright/test";
import * as fsPromises from "node:fs/promises";
import * as path from "node:path";
type MyFixtures = {
_withCoverage: undefined;
};
export const test = base.extend<MyFixtures>({
_withCoverage: async ({ page }, use, testInfo) => {
await page.coverage.startJSCoverage();
await use(undefined);
const coverage = await page.coverage.stopJSCoverage();
const srcPath = "@fs" + path.normalize(`${__dirname}/../../src`);
const srcCoverage = coverage
.filter((entry) => entry.url.includes(srcPath))
.map((entry) => {
return { ...entry, url: entry.url.replace(/^.+@fs/, "file://") };
});
await fsPromises.mkdir("coverage/tmp", { recursive: true });
const testTitle = testInfo.title.replaceAll("/", "_");
await fsPromises.writeFile(
`coverage/tmp/${testTitle}.json`,
JSON.stringify({ result: srcCoverage }, null, 2),
);
},
});
export { expect } from "@playwright/test"; Then in your test files: import { test, expect } from "./fixtures.ts";
test("test1", async ({ page, _withCoverage }) => {
await page.goto("http://localhost:5173/");
});
test("test2", async ({ page, _withCoverage }) => {
await page.goto("http://localhost:5173/");
}); After running |
@jennydaman It is actually improving on my example ! Do you think you can make it into a Github repository for people to try it out and contribute ? |
I couldn't figure it out how to do so. I wanted to try extending
EDIT: I found the workaround here #15819, will create the package! |
I have developed a minimal package which configures Playwright to collect test coverage and write the data to a directory. https://www.npmjs.com/package/playwright-test-coverage-native |
@merishabhgupta I have the exact use case that you had, were you able to find any solution? |
If you are looking for a JavaScript code coverage tool, You could try monocart-coverage-reports
Especially for Playwright, it has been seamlessly integrated into the custom reporter monocart-reporter, see Code Coverage Report |
I am at hour of deciding that whether should I select cypress or playwright. This feature is very important for me to tackle like cypress is doing here. |
Playwright really needs to provide better support for coverage reporting. The current documentation is really lackluster and folk need to scavenge the internet for anything they can find on how to properly implement this. Since Microsoft appears to have been unsure on why people want this in the first place, let me explain. Coverage is (albeit not perfect) an important metric to teams to check whether workflows have been tested thorougly, and UI tests are no exception to this. When it comes to frontends in particular, many teams rely heavily on integration and UI tests, which are hard to get done properly with existing (unit) test frameworks like vitest. In our team, we use vitest for unit-focused testing and Playwright for integrated and UI testing. If coverage reports only cover e.g. vitest tests, reviewers have to guess whether any uncovered code might be covered by UI tests. If we get coverage reports from both playwright and vitest, we can even merge the reports with tools like Istanbul to get the full picture. This is extremely beneficial to automated quality checks. So please, Microsoft, don't neglect proper coverage reporting. Playwright really needs to provide a better API and documentation on coverage reporting, just like e.g. vitest does. |
ok, i wanted to add code coverage report for my project to work with sonar-qube, but i see that is not that easy... |
It would be great if playwright can provide OOTB support for calculating the code coverage of e2e tests.
This feature is provided by every test framework, as we are migrating to playwright from wdio, this would be a great help if this feature is available.
The text was updated successfully, but these errors were encountered: