forked from cenfun/monocart-coverage-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-puppeteer.js
154 lines (123 loc) · 3.79 KB
/
test-puppeteer.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const EC = require('eight-colors');
const MCR = require('../');
const checkSnapshot = require('./check-snapshot.js');
// v8 and lcov
const coverageOptions = {
// logging: 'debug',
// watermarks: [60, 90],
reports: [
['console-summary', {
// metrics: ['bytes', 'functions', 'lines']
}],
['v8', {
// metrics: ['bytes', 'functions', 'lines']
}],
'v8-json'
],
name: 'My Puppeteer Coverage Report',
assetsPath: '../assets',
lcov: true,
sourceFilter: (sourcePath) => sourcePath.search(/src\//) !== -1 || sourcePath.search(/minify\//) !== -1,
sourcePath: (filePath) => {
const map = {
'localhost-8130/': 'test/mock/',
'coverage-v8/': ''
};
for (const key in map) {
if (filePath.startsWith(key)) {
return map[key] + filePath.slice(key.length);
}
}
return filePath;
},
all: 'test/mock/src',
outputDir: './docs/puppeteer',
onEnd: function(coverageResults) {
checkSnapshot(coverageResults);
}
};
const test = async () => {
console.log('start puppeteer test ...');
const browser = await puppeteer.launch({
// headless: false,
args: ['--no-sandbox']
});
const page = await browser.newPage();
await Promise.all([
page.coverage.startJSCoverage({
resetOnNavigation: false,
includeRawScriptCoverage: true
}),
page.coverage.startCSSCoverage({
resetOnNavigation: false
})
]);
// Playwright: By default, the Playwright tests run on a default viewport size of 1280x720 .
// Puppeteer: Defaults to an 800x600 viewport.
await page.setViewport({
width: 1280,
height: 720,
deviceScaleFactor: 1
});
const fileList = [
'./test/mock/v8/index.html',
'./test/mock/v8/dist/coverage-v8.js',
'./test/mock/css/style.css'
];
for (const filePath of fileList) {
const content = fs.readFileSync(filePath).toString('utf-8');
const extname = path.extname(filePath);
if (extname === '.html') {
await page.setContent(content);
} else if (extname === '.css') {
await page.addStyleTag({
content: `${content}\n/*# sourceURL=${filePath} */`
});
} else {
await page.addScriptTag({
content: `${content}\n//# sourceURL=${filePath}`
});
}
}
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await page.evaluate(() => {
const { foo } = window['coverage-v8'];
foo();
});
await page.evaluate(() => {
const { bar } = window['coverage-v8'];
bar();
});
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage()
]);
// to raw V8 script coverage
const coverageList = [... jsCoverage.map((it) => {
return {
source: it.text,
... it.rawScriptCoverage
};
}), ... cssCoverage];
// v8
const report = await MCR(coverageOptions).add(coverageList);
console.log('puppeteer coverage added', report.type);
await browser.close();
};
const generate = async () => {
console.log('generate puppeteer coverage reports ...');
const coverageResults = await MCR(coverageOptions).generate();
console.log('puppeteer coverage reportPath', EC.magenta(coverageResults.reportPath));
};
const main = async () => {
// clean cache first
await MCR(coverageOptions).cleanCache();
await test();
await generate();
};
main();