Registry / testing / playwright-test-coverage

playwright-test-coverage

JSON →
library1.2.12jsnpmunverified

Playwright Test Coverage is an extension for the Playwright test framework designed to facilitate code coverage collection from end-to-end tests. Currently stable at version 1.2.12, this library integrates with `babel-plugin-istanbul` for source code instrumentation and `nyc` for managing the test run and generating comprehensive coverage reports. Unlike some other coverage tools, `playwright-test-coverage` requires developers to explicitly instrument their frontend source code during the build process and run their application server via `nyc`. It provides a wrapper around Playwright's `test` and `expect` functions, ensuring that coverage data from the browser context is properly captured and merged. This setup is particularly useful for projects that already leverage Babel for their build pipeline and require detailed E2E coverage metrics.

npm install playwright-test-coverage
INSTALL
IMPORT
SIG · PLAYWRIGHT-TEST-CO
P
playwright-test-coverage
testingjavascriptv1.2.12
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

test, expect
import { test, expect } from 'playwright-test-coverage';
import test from 'playwright-test-coverage';
For ESM projects, use named imports. This package exports `test` and `expect` as named exports.
test, expect
const { test, expect } = require('playwright-test-coverage');
const playwright = require('playwright-test-coverage'); const test = playwright.test;
The primary documentation and examples use CommonJS `require` for `test` and `expect`.
Test, Expect
import type { Test, Expect } from 'playwright-test-coverage';
When using TypeScript, import the types separately for `test` and `expect` if needed for custom test contexts or extensions.

Demonstrates how to import `test` and `expect` from `playwright-test-coverage` to enable coverage collection in a basic Playwright E2E test, including necessary setup steps.

const { test, expect } = require("playwright-test-coverage"); test("basic test with coverage", async ({ page }) => { // Assume your application is running on localhost:3000, // instrumented with babel-plugin-istanbul, and started via nyc. await page.goto("http://localhost:3000/"); // Example: Interact with an element to trigger code execution await page.locator('button').click(); // Assertions as usual const title = page.locator('h1'); await expect(title).toHaveText("Welcome to My App"); }); // To run: // 1. Ensure your frontend code is instrumented with babel-plugin-istanbul. // 2. Start your server with nyc: `nyc node your-server-script.js` // 3. Run your Playwright tests: `npx playwright test`
Debug
Known issues
gotchaCode coverage is not collected or appears empty, despite tests running.
fix
Ensure your frontend source code is correctly instrumented with `babel-plugin-istanbul` during your build process. This package does not instrument your code automatically.
affects: >=1.0.0
gotchaCoverage data files are not generated in the `.nyc_output` directory (or custom `ISTANBUL_TEMP_DIR`).
fix
You must run your application server (the one being tested by Playwright) through `nyc`. For example, instead of `node server.js`, use `nyc node server.js`.
affects: >=1.0.0
gotcha`test` and `expect` functionalities seem to work, but no coverage data is associated with them.
fix
Always import `test` and `expect` directly from `playwright-test-coverage` (e.g., `const { test, expect } = require("playwright-test-coverage")`) and NOT from `@playwright/test`. This ensures the coverage-enabled wrappers are used.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: test is not a function
Attempting to call `test` after incorrectly importing it, likely from `@playwright/test` directly instead of `playwright-test-coverage`.
fix
Ensure `test` and `expect` are imported from `playwright-test-coverage`: `const { test, expect } = require("playwright-test-coverage");` or `import { test, expect } from "playwright-test-coverage";`
Error: ENOENT: no such file or directory, stat '.nyc_output/index.json'
NYC cannot find coverage data because the application was not run in an instrumented environment or no coverage data was captured.
fix
Verify that your application's source code is instrumented with `babel-plugin-istanbul` and that your server is started using `nyc` (e.g., `nyc npm start` or `nyc node server.js`). Also, ensure tests interact with the instrumented code.
Upgrade
Version history
1.2.12latest on npm
Audit
Dependencies
@playwright/testrequiredRequired as the underlying Playwright test framework.
babel-plugin-istanbulrequiredMandatory for instrumenting frontend source code to collect coverage.
nycrequiredRequired for running the application server to capture coverage data and generate reports.
Agent activity
4 hits · last 30 days
node
4
Resources
playwright-test-coverage — npm install playwright-test-coverage · libregistry