@bgotink/playwright-coverage

Report coverage on playwright tests using v8 coverage, without requiring any instrumentation.
Usage
Install this package
yarn add -D @bgotink/playwright-coverage
Then add the reporter to your playwright configuration:
import {defineCoverageReporterConfig} from '@bgotink/playwright-coverage';
import {defineConfig} from '@playwright/test';
export default defineConfig({
reporter: [
['list'],
[
'@bgotink/playwright-coverage',
defineCoverageReporterConfig({
sourceRoot: __dirname,
exclude: ['path/to/ignored/code/**'],
resultDir: path.join(__dirname, 'results/e2e-coverage'),
reports: [
['html'],
[
'lcovonly',
{
file: 'coverage.lcov',
},
],
[
'text-summary',
{
file: null,
},
],
],
}),
],
],
});
Now replace all calls to @playwright/test
's test
variable with a variant that tracks coverage.
The easiest way to do this is by importing test
from @bgotink/playwright-coverage
instead.
-import {expect, test} from '@playwright/test';
+import {expect, test} from '@bgotink/playwright-coverage';
If you're already using a different test
function, e.g. if you're using @ngx-playwright/test
, you can add coverage tracking using the mixinFixtures
function:
import {test as base} from '@ngx-playwright/test';
import {mixinFixtures as mixinCoverage} from '@bgotink/playwright-coverage';
export const test = mixinCoverage(base);
or you can use mergeTests
if you're using playwright ≥ 1.40.0:
import {mergeTests} from '@playwright/test';
import {test as testWithCoverage} from '@bgotink/playwright/coverage';
import {test as otherTest} from '@ngx-playwright/test';
export const test = mergeTests(
testWithCoverage,
otherTest,
);
Now replace all usage of test
with the function export defined there, and coverage will be tracked.
How does it work?
The fixtures registered in test
or via mixinFixtures
hook into created Page
s to track javascript coverage with v8. The coverage data is added as attachment to every test.
Upon completion of all tests, the reporter merges all generated coverage files into one and then converts the v8 coverage format into the coverage format used by istanbul. The istanbul data is then passed into the reports of istanbul-reports
.
Common issues
The HTML report shows errors saying the source files couldn't be read
This means the reporter is looking in the wrong place because playwright and the server process are using paths relative to a different working folder.
Try setting the sourceRoot
folder. If you need more control over the actual path of the files, pass a rewritePath
property in the options:
{
sourceRoot: __dirname,
rewritePath: ({absolutePath, relativePath}) => {
return absolutePath;
},
}
Coverage is empty
Did you perhaps use @playwright/test
's own test
function?
If you don't use a test
function created using mixinCoverage
, coverage won't be tracked and the reporter won't have anything to report on.
Status
This project is very experimental. It has been proven to work on one angular application, i.e. with webpack with the unmodified configuration angular applies to it.
License
Licensed under the MIT license, see LICENSE.md
.