{"id":13777,"library":"playwright-test-coverage","title":"Playwright Test Coverage","description":"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.","status":"active","version":"1.2.12","language":"javascript","source_language":"en","source_url":"https://github.com/anishkny/playwright-test-coverage","tags":["javascript","playwright","coverage","istanbul","babel","typescript"],"install":[{"cmd":"npm install playwright-test-coverage","lang":"bash","label":"npm"},{"cmd":"yarn add playwright-test-coverage","lang":"bash","label":"yarn"},{"cmd":"pnpm add playwright-test-coverage","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the underlying Playwright test framework.","package":"@playwright/test","optional":false},{"reason":"Mandatory for instrumenting frontend source code to collect coverage.","package":"babel-plugin-istanbul","optional":false},{"reason":"Required for running the application server to capture coverage data and generate reports.","package":"nyc","optional":false}],"imports":[{"note":"For ESM projects, use named imports. This package exports `test` and `expect` as named exports.","wrong":"import test from 'playwright-test-coverage';","symbol":"test, expect","correct":"import { test, expect } from 'playwright-test-coverage';"},{"note":"The primary documentation and examples use CommonJS `require` for `test` and `expect`.","wrong":"const playwright = require('playwright-test-coverage');\nconst test = playwright.test;","symbol":"test, expect","correct":"const { test, expect } = require('playwright-test-coverage');"},{"note":"When using TypeScript, import the types separately for `test` and `expect` if needed for custom test contexts or extensions.","symbol":"Test, Expect","correct":"import type { Test, Expect } from 'playwright-test-coverage';"}],"quickstart":{"code":"const { test, expect } = require(\"playwright-test-coverage\");\n\ntest(\"basic test with coverage\", async ({ page }) => {\n  // Assume your application is running on localhost:3000,\n  // instrumented with babel-plugin-istanbul, and started via nyc.\n  await page.goto(\"http://localhost:3000/\");\n  // Example: Interact with an element to trigger code execution\n  await page.locator('button').click();\n\n  // Assertions as usual\n  const title = page.locator('h1');\n  await expect(title).toHaveText(\"Welcome to My App\");\n});\n\n// To run:\n// 1. Ensure your frontend code is instrumented with babel-plugin-istanbul.\n// 2. Start your server with nyc: `nyc node your-server-script.js`\n// 3. Run your Playwright tests: `npx playwright test`","lang":"javascript","description":"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."},"warnings":[{"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.","message":"Code coverage is not collected or appears empty, despite tests running.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"Coverage data files are not generated in the `.nyc_output` directory (or custom `ISTANBUL_TEMP_DIR`).","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"`test` and `expect` functionalities seem to work, but no coverage data is associated with them.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.2.12':31 'alreadi':117 'applic':81 'around':89 'babel':37,119,134 'babel-plugin-istanbul':36 'browser':102 'build':76,122 'captur':106 'code':18,42,73 'collect':20 'comprehens':53 'context':103 'coverag':3,6,19,54,59,64,98,128,132 'current':27 'data':99 'design':15 'detail':126 'develop':66 'e2e':127 'end':23,25 'end-to-end':22 'ensur':96 'expect':94 'explicit':68 'extens':9 'facilit':17 'framework':14 'frontend':71 'function':95 'generat':52 'instrument':43,69 'integr':34 'istanbul':39,133 'javascript':130 'leverag':118 'librari':33 'manag':47 'merg':108 'metric':129 'nyc':45,84 'particular':112 'pipelin':123 'playwright':1,4,12,62,90,131 'playwright-test-coverag':61 'plugin':38 'process':77 'project':115 'proper':105 'provid':86 'report':55 'requir':65,125 'run':50,79 'server':82 'setup':110 'sourc':41,72 'stabl':28 'test':2,5,13,26,49,63,92 'tool':60 'typescript':135 'unlik':56 'use':113 'version':30 'via':83 'wrapper':88","created_at":"2026-04-20T01:56:15.429943+00:00","updated_at":"2026-04-20T01:56:15.429943+00:00","problems":[{"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\";`","cause":"Attempting to call `test` after incorrectly importing it, likely from `@playwright/test` directly instead of `playwright-test-coverage`.","error":"TypeError: test is not a function"},{"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.","cause":"NYC cannot find coverage data because the application was not run in an instrumented environment or no coverage data was captured.","error":"Error: ENOENT: no such file or directory, stat '.nyc_output/index.json'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/anishkny/playwright-test-coverage","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/playwright-test-coverage","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}