-
Notifications
You must be signed in to change notification settings - Fork 90
/
code-coverage.js
40 lines (31 loc) · 1.04 KB
/
code-coverage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Gather coverage for JS and CSS files
await Promise.all([page.coverage.startJSCoverage(), page.coverage.startCSSCoverage()]);
await page.goto('https://pptr.dev');
// Stops the coverage gathering
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
// Calculates # bytes being used based on the coverage
const calculateUsedBytes = (type, coverage) =>
coverage.map(({url, ranges, text}) => {
let usedBytes = 0;
ranges.forEach((range) => (usedBytes += range.end - range.start - 1));
return {
url,
type,
usedBytes,
totalBytes: text.length,
percentUsed: `${(usedBytes / text.length * 100).toFixed(2)}%`
};
});
console.info([
...calculateUsedBytes('js', jsCoverage),
...calculateUsedBytes('css', cssCoverage),
]);
await browser.close();
})();