'Get zero coverage with nyc and playwright
I'm struggling to set up coverage correctly using Playwright. It reports 0 coverage in all files (except the test files themselves, if I include them).
I'm getting inspiration from https://playwright.dev/docs/api/class-coverage and https://github.com/bgotink/playwright-coverage/blob/main/src/fixtures.ts. Our project is a monorepo where tests in a folder e2e-tests/ run end to end tests on servers contained in other adjacent folders, e.g. frontend/.
The current setup is using a page fixture like so in each test-file:
// frontend.spec.ts
import { test, expect } from "../fixtures";
test("something", ({ page ) => {
// Do test stuff with page
});
where the fixture is defined as
// fixtures/page.ts
import { Page, TestInfo } from "@playwright/test";
const pageWithCoverage = async (
{ page, browserName }: { page: Page; browserName: string },
use: (page: Page) => Promise<void>,
testInfo: TestInfo
) => {
if (!page.coverage) throw new Error(`Could not collect coverage with browser "${browserName}"`);
console.log("📈 Collecting coverage");
await page.coverage.startJSCoverage();
await use(page);
await page.coverage.stopJSCoverage();
};
export default pageWithCoverage;
To collect coverage I run
npx nyc --all --cwd ".." --include "**/frontend/**/* --nycrc-path e2e-tests/.nycrc npm t
where the relevant part concerning the file structure is:
--all --cwd ".." --include "**/frontend/**/*"
I'm using a .nycrc file containing nyc-config-tsx in order to instrument tsx files:
// .nycrc
{
"extends": "nyc-config-tsx",
"all": true
}
Can you tell what the issue is?
The frontend is built using next.
I get similar results storing results to files using v8toIstanbul and running npx nyc report
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
