Hi I need help getting codecoverage for my typscript/Lit code,
I have setup my tests using istanbul and getting coverage summary statements unknown ( 0/0 ),
Here's the steps I followed,
Istalled Istanbul: npm install --save-dev nyc
Created the .nycrc file and have this: { "include": ["./test/e2e//*.spec.ts", "../src//*.ts"], "exclude": ["./test/unit", "node_modules"], "report-dir": "coverage/playwright", "reporter": ["lcov", "html"], "all": true, "check-coverage": true, "per-file": true, "extension": [".ts"] },
And running the command like this: npx nyc --reporter=lcov --report-dir=coverage/playwright npm run test:e2e
This thread is trying to answer question "How can I get code coverage for my TypeScript/Lit code using Istanbul with Playwright tests?"
I have a BaseFixtures.ts file like this: import * as fs from 'fs'; import * as path from 'path'; import * as crypto from 'crypto'; import { test as baseTest } from '@playwright/test';
const istanbulCLIOutput = path.join(process.cwd(), '.nyc_output');
export function generateUUID(): string { return crypto.randomBytes(16).toString('hex'); }
export const test = baseTest.extend({ context: async ({ context }, use) => { await context.addInitScript(() => window.addEventListener('beforeunload', () => (window as any).collectIstanbulCoverage(JSON.stringify((window as any).coverage)) ), ); await fs.promises.mkdir(istanbulCLIOutput, { recursive: true }); await context.exposeFunction('collectIstanbulCoverage', (coverageJSON: string) => { if (coverageJSON) fs.writeFileSync(path.join(istanbulCLIOutput, playwright_coverage_${generateUUID()}.json), coverageJSON); }); await use(context); for (const page of context.pages()) { await page.evaluate(() => (window as any).collectIstanbulCoverage(JSON.stringify((window as any).coverage))) } } });
export const expect = test.expect; ...I followed this repo: https://github.com/mxschmitt/playwright-test-coverage/blob/main/e2e/baseFixtures.ts GitHub playwright-test-coverage/e2e/baseFixtures.ts at main · mxschmitt/pl... Playwright Test (@playwright/test) demo to collect coverage information via Istanbul - mxschmitt/playwright-test-covera
For future reference you can try to check out these guides:
Playwright code coverage with monocart-reporter: https://github.com/edumserrano/playwright-adventures/blob/main/demos/code-coverage-with-monocart-reporter/README.md
Playwright code coverage with Istanbul via Webpack Babel plugin https://github.com/edumserrano/playwright-adventures/blob/main/demos/code-coverage-with-istanbul-via-webpack-babel-plugin/README.md
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].