Skip to content

Commit f79d2ee

Browse files
committed
fix readme
1 parent 78266c3 commit f79d2ee

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,7 @@ In Edge, Chrome or chromium you can manually collect coverage in the browser's D
4545

4646
Additionally, DevTools Tips writes about it in their [explainer](https://devtoolstips.org/tips/en/detect-unused-code/).
4747

48-
### Coverage API
49-
50-
Both Puppeteer and Playwright provide an API to programmatically get the coverage data, allowing you to put that directly into this library. Here is the gist:
51-
52-
```ts
53-
// Start collecting coverage
54-
await page.coverage.startCSSCoverage()
55-
// Load the page, do all sorts of interactions to increase coverage, etc.
56-
await page.goto('http://example.com')
57-
// Stop the coverage and store the result in a variable to pass along
58-
let coverage = await page.coverage.stopCSSCoverage()
59-
60-
// Now we can process it
61-
import { calculate_coverage } from '@projectwallace/css-code-coverage'
62-
63-
function parse_html(html) {
64-
return new DOMParser().parseFromString(html, 'text/html')
65-
}
66-
67-
let report = calculcate_coverage(coverage, parse_html)
68-
```
69-
70-
1. Via the `coverage.startCSSCoverage()` API that headless browsers like [Playwright](https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage) or [Puppeteer](https://pptr.dev/api/puppeteer.coverage.startcsscoverage/) provide.
71-
72-
Either way you end up with one or more JSON files that contain coverage data.
48+
You end up with one or more JSON files that contain coverage data. We provide a helper `parse_coverage()` that both parses the JSON and validates it so you can pass it directly into `calculate_coverage()`.
7349

7450
```ts
7551
// Read a single JSON or a folder full of JSON files with coverage data
@@ -92,6 +68,28 @@ for (let file of files) {
9268
}
9369
```
9470

71+
### Coverage API
72+
73+
Both Puppeteer and Playwright provide an API to programmatically get the coverage data, allowing you to put that directly into this library. Here is the gist:
74+
75+
```ts
76+
// Start collecting coverage
77+
await page.coverage.startCSSCoverage()
78+
// Load the page, do all sorts of interactions to increase coverage, etc.
79+
await page.goto('http://example.com')
80+
// Stop the coverage and store the result in a variable to pass along
81+
let coverage = await page.coverage.stopCSSCoverage()
82+
83+
// Now we can process it
84+
import { calculate_coverage } from '@projectwallace/css-code-coverage'
85+
86+
function parse_html(html) {
87+
return new DOMParser().parseFromString(html, 'text/html')
88+
}
89+
90+
let report = calculcate_coverage(coverage, parse_html)
91+
```
92+
9593
### Optional: coverage from `<style>` blocks
9694

9795
Coverage generators also create coverage ranges for `<style>` blocks in HTML. If this applies to your code you should provide a HTML parser that we use to 'scrape' the HTML in case the browser gives us not just plain CSS contents. Depending on where you run this analysis you can use:

0 commit comments

Comments
 (0)