Skip to content

Commit

Permalink
coverage generation for playwright tests
Browse files Browse the repository at this point in the history
only generates the istanbul coverage for the initial page object in
the main context fixture.
Still missing is coverage for the auth setup, coverage for any popup
pages (like the sidebar pages) and the background worker.
  • Loading branch information
fungairino committed Apr 30, 2024
1 parent 92fae7a commit b0200ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
24 changes: 24 additions & 0 deletions end-to-end-tests/fixtures/extensionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
} from "./utils";
import { ModsPage } from "../pageObjects/extensionConsole/modsPage";
import { test as envSetup } from "./envSetup";
import v8toIstanbul from "v8-to-istanbul";
import { v4 } from "uuid";

// This environment variable is used to attach the browser sidepanel window that opens automatically to Playwright.
// See https://github.com/microsoft/playwright/issues/26693
Expand All @@ -38,6 +40,16 @@ process.env.PW_CHROMIUM_ATTACH_TO_OTHER = "1";
// See https://playwright.dev/docs/service-workers-experimental
process.env.PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS = "1";

const coverageDirectory = path.join(__dirname, "../.output/coverage");

const collectCoverage = async (coverageJSON: string) => {
if (coverageJSON)
await fs.writeFile(
`${coverageDirectory}/playwright_coverage_${v4()}.json`,
coverageJSON,
);
};

export const test = mergeTests(
envSetup,
base.extend<
Expand Down Expand Up @@ -83,6 +95,10 @@ export const test = mergeTests(
recursive: true,
});

await fs.mkdir(coverageDirectory, {
recursive: true,
});

const context = await launchPersistentContextWithExtension(
chromiumChannel,
temporaryProfileDirectory,
Expand All @@ -94,12 +110,20 @@ export const test = mergeTests(
async page({ context, extensionId }, use) {
// Re-use the initial context page if it exists
const page = context.pages()[0] || (await context.newPage());
await page.coverage.startJSCoverage();

// Start off test from the extension console, and ensure it is done loading
const modsPage = new ModsPage(page, extensionId);
await modsPage.goto();

await use(page);
const coverage = await page.coverage.stopJSCoverage();
for (const entry of coverage) {
const converter = v8toIstanbul("", 0, { source: entry.source });
await converter.load();

Check failure on line 123 in end-to-end-tests/fixtures/extensionBase.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected `await` inside a loop
converter.applyCoverage(entry.functions);
await collectCoverage(JSON.stringify(converter.toIstanbul()));

Check failure on line 125 in end-to-end-tests/fixtures/extensionBase.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected `await` inside a loop
}
// The page is closed by the context fixture `.close` cleanup step
},
async extensionId({ context }, use) {
Expand Down
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
"type-fest": "^4.18.0",
"typescript": "^5.4.5",
"typescript-plugin-css-modules": "^5.1.0",
"v8-to-istanbul": "^9.2.0",
"webpack": "^5.91.0",
"webpack-build-notifier": "^2.3.0",
"webpack-bundle-analyzer": "^4.10.2",
Expand Down

0 comments on commit b0200ca

Please sign in to comment.