Skip to content

Commit c411bc6

Browse files
committed
Remove playwright subpath export, add fixture recipe to README
The subpath added no real value once reduced to a plain helper function. The full fixture setup is simple enough to paste directly, and keeping it in the README means users own the code and can adapt it freely.
1 parent 22380ff commit c411bc6

5 files changed

Lines changed: 24 additions & 159 deletions

File tree

‎README.md‎

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,67 +58,59 @@ let report = calculcate_coverage(coverage)
5858

5959
### Playwright fixture
6060

61-
Use `save_css_coverage` to collect and save CSS coverage from within a Playwright fixture.
62-
63-
**1. Add a fixture** (e.g. `tests/fixtures.ts`):
61+
Add a `cssCoverage` fixture to automatically collect and save CSS coverage for each test. Create a fixtures file (e.g. `tests/fixtures.ts`):
6462

6563
```ts
6664
import { test as base, expect } from '@playwright/test'
67-
import { save_css_coverage } from '@projectwallace/css-code-coverage/playwright'
65+
import * as fs from 'node:fs/promises'
66+
import * as path from 'node:path'
6867

6968
export const test = base.extend({
7069
cssCoverage: [
7170
async ({ page }, use, testInfo) => {
7271
await page.coverage.startCSSCoverage()
7372
await use()
7473
let coverage = await page.coverage.stopCSSCoverage()
75-
await save_css_coverage(coverage, {
76-
title_path: testInfo.titlePath,
77-
attach: testInfo.attach.bind(testInfo),
78-
})
74+
75+
let file_name =
76+
testInfo.titlePath
77+
.map((s) =>
78+
s
79+
.replaceAll(/\s+|\/|\./g, '-')
80+
.replaceAll(/[^a-z0-9-_]/gi, '')
81+
.toLowerCase(),
82+
)
83+
.join('-') + '.json'
84+
85+
let dir = path.join(process.cwd(), 'css-coverage')
86+
await fs.mkdir(dir, { recursive: true })
87+
let file_path = path.join(dir, file_name)
88+
await fs.writeFile(file_path, JSON.stringify(coverage))
89+
await testInfo.attach('css-coverage', { path: file_path, contentType: 'application/json' })
7990
},
8091
{},
8192
],
8293
})
8394
export { expect }
8495
```
8596

86-
**2. Use `cssCoverage` in any test** where you want to collect coverage:
97+
Use `cssCoverage` in any test where you want to collect coverage:
8798

8899
```ts
89100
import { test, expect } from './fixtures.js'
90101

91102
test('my test', async ({ page, cssCoverage }) => {
92103
await page.goto('https://example.com')
93-
// CSS coverage is collected and written to disk when the test finishes
94104
})
95105
```
96106

97-
The fixture starts coverage when the test begins and stops it when the test ends. JSON files are written to the output directory and attached to the Playwright HTML report.
98-
99-
**Enable for an entire test file** with a single line:
107+
Or enable it for an entire file with a single line:
100108

101109
```ts
102-
import { test, expect } from './fixtures.js'
103-
104110
test.beforeEach(async ({ cssCoverage }) => {})
105-
106-
test('first test', async ({ page }) => { ... })
107-
test('second test', async ({ page }) => { ... })
108111
```
109112

110-
**Configure the output directory** (optional) via the `dir` option:
111-
112-
```ts
113-
await save_css_coverage(coverage, {
114-
dir: 'css-coverage', // default
115-
title_path: testInfo.titlePath,
116-
})
117-
```
118-
119-
Or set it globally in `playwright.config.ts` by passing the configured path to `save_css_coverage` from a shared fixture.
120-
121-
Pass the directory to the `css-coverage` CLI to analyze results:
113+
Pass the output directory to the `css-coverage` CLI to analyze results:
122114

123115
```sh
124116
css-coverage --coverage-dir=./css-coverage --min-coverage=0.8

‎package.json‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@
3030
"main": "dist/index.js",
3131
"types": "dist/index.d.ts",
3232
"exports": {
33-
".": {
34-
"types": "./dist/index.d.ts",
35-
"default": "./dist/index.js"
36-
},
37-
"./playwright": {
38-
"types": "./dist/playwright/index.d.mts",
39-
"default": "./dist/playwright/index.mjs"
40-
}
33+
"types": "./dist/index.d.ts",
34+
"default": "./dist/index.js"
4135
},
4236
"publishConfig": {
4337
"access": "public",

‎src/playwright.test.ts‎

Lines changed: 0 additions & 74 deletions
This file was deleted.

‎src/playwright/index.ts‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎tsdown.config.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ export default defineConfig([
1616
}),
1717
],
1818
},
19-
{
20-
entry: 'src/playwright/index.ts',
21-
platform: 'node',
22-
format: 'esm',
23-
outDir: 'dist/playwright',
24-
publint: true,
25-
plugins: [
26-
codecovVitePlugin({
27-
enableBundleAnalysis: process.env.CODECOV_TOKEN !== undefined,
28-
bundleName: 'playwright.js',
29-
uploadToken: process.env.CODECOV_TOKEN,
30-
telemetry: false,
31-
}),
32-
],
33-
},
3419
{
3520
entry: 'src/cli/cli.ts',
3621
platform: 'node',

0 commit comments

Comments
 (0)