-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
harness.test.ts
90 lines (76 loc) · 2.51 KB
/
harness.test.ts
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
//
// mdn-bcd-collector: unittest/puppeteer/harness.test.ts
// Unittest for testing harness.js in multiple browsers
//
// © Gooborg Studios, Google LLC, Mozilla Corporation
// See the LICENSE file for copyright details
//
import {fileURLToPath} from "node:url";
import {assert} from "chai";
import fs from "fs-extra";
import puppeteer, {Product} from "puppeteer";
import {app} from "./app.js";
// Firefox is temporarily disabled due to issues on CI
const products: Product[] = ["chrome"]; // ['chrome', 'firefox'];
// Workaround for https://github.com/puppeteer/puppeteer/issues/6255
const consoleLogType = {
chrome: "log",
firefox: "verbose",
};
describe("harness.js", () => {
const port = process.env.PORT || 8081;
let server;
before(() => {
server = app.listen(port);
});
after(() => server.close());
for (const product of products) {
it(product, async () => {
const browser = await puppeteer.launch({product});
after(() => browser.close());
const page = await browser.newPage();
if (product == "chrome") {
await page.coverage.startJSCoverage({includeRawScriptCoverage: true});
}
const reportPromise = new Promise((resolve, reject) => {
page.on("console", (msg) => {
if (msg.type() === consoleLogType[product]) {
resolve(JSON.parse(msg.text()));
}
});
page.on("error", reject);
});
await page.goto(`http://localhost:${port}/unittest/#reporter=json`);
const report: any = await reportPromise;
if (product == "chrome") {
const jsCoverage = await page.coverage.stopJSCoverage();
// Adjust coverage reports to point to original files
// and filter for non-generated files
const coverage = jsCoverage
.map(
({rawScriptCoverage: it}) =>
it && {
...it,
scriptId: String(it.scriptId),
url: it.url.replace(
`http://localhost:${port}/`,
new URL("./static/", import.meta.url).toString(),
),
},
)
.filter((it) => it && fs.existsSync(fileURLToPath(it.url)));
coverage.forEach((it, idx) =>
fs.writeFileSync(
`${
process.env.NODE_V8_COVERAGE
}/coverage-${Date.now()}-${idx}.json`,
JSON.stringify({result: [it]}),
),
);
}
assert.equal(report.stats.failures, 0);
})
.slow(10000)
.timeout(30000);
}
});