forked from scala-js/scala-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-html.js
83 lines (65 loc) · 2.06 KB
/
test-html.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
const express = require("express");
const http = require("http");
const process = require("process");
const { JSDOM } = require("jsdom");
const servingDirectory = process.argv[2];
const requestPath = process.argv[3];
console.log(`serving: ${servingDirectory}`);
serveDirectory(servingDirectory).then(server => {
const url = makeURL(server.address(), requestPath);
console.log(`loading ${url}`);
JSDOM.fromURL(url, {
resources: "usable",
runScripts: "dangerously",
}).then(waitComplete)
.finally(() => server.close());
}).catch(err => {
console.error(err);
process.exit(1);
});
function waitComplete(dom) {
const boxes = dom.window.document.getElementsByClassName("test-box-header");
const errors = [];
let completed = 0;
for (const box of boxes) {
const cls = box.className;
if (cls === "test-box-header error") {
errors.push(box.innerHTML);
} else if (cls === "test-box-header success") {
completed++;
} else if (cls === "test-box-header") {
// pending
} else {
return Promise.reject(
new Error(`unknown header class ${cls}. element:\n${box.innerHTML}`));
}
}
if (errors.length !== 0) {
errors.shift("tests failed:");
return Promise.reject(new Error(errors.join("\n")));
}
const total = boxes.length;
console.log(`${completed}/${total} tests`);
if (total > 0 && completed === total) {
return Promise.resolve(void 0);
}
return new Promise(res => setTimeout(res, 100))
.then(() => waitComplete(dom));
}
function serveDirectory(dir) {
const app = express();
app.use(express.static(dir));
const server = http.createServer(app);
return new Promise((res, rej) => {
server.listen(() => res(server));
server.on("error", rej);
});
}
function makeURL(serverAddress, path) {
const { port, address, family } = serverAddress;
if (family === "IPv4")
return `http://${address}:${port}/${path}`
if (family === "IPv6")
return `http://[${address}]:${port}/${path}`
throw new Error(`do not know how to construct URL for address family ${family}`);
}