-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
check-esm-bundle-is-runnable.js
87 lines (70 loc) · 2.18 KB
/
check-esm-bundle-is-runnable.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
/* eslint-disable no-process-exit */
const puppeteer = require("puppeteer");
const http = require("http");
const fs = require("fs");
const port = 3876;
const htmlWithModuleScript = `
<script type="module">
import sinon from '/sinon-esm.js';
const assert = (result) => { if(!result) throw new Error("Failed test"); };
try {
const stub = sinon.stub().returns(42);
assert(42 === stub());
console.log('sinon-result:works');
} catch(err) {
console.log('sinon-result:fails Assertion incorrect' );
}
</script>
`;
// start server where our built sinon esm bundle resides
process.chdir(`${__dirname}/../../pkg/`);
const sinonModule = fs.readFileSync("./sinon-esm.js");
async function evaluatePageContent() {
const browser = await puppeteer.launch({
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-on-travis-ci
args: ["--no-sandbox"],
// allow overriding chrome path
executablePath: process.env.SINON_CHROME_BIN || null
});
const page = await browser.newPage();
function die(reason) {
if (reason) {
/* eslint-disable no-console */
console.error(reason);
}
browser.close();
process.exit(1);
}
page.on("error", function (err) {
throw err;
});
// our "assertion framework" :)
page.on("console", function (msg) {
var text = msg.text();
if (text.startsWith("sinon-result:works")) {
browser.close();
process.exit(0);
} else if (text.startsWith("sinon-result:fails")) {
die(text);
}
});
await page.goto("http://localhost:3876");
setTimeout(() => die("No result within timeout."), 1000);
}
const app = http.createServer((req, res) => {
let body, type;
if (req.url.match(/sinon-esm\.js/)) {
body = sinonModule;
type = "application/javascript";
} else {
body = htmlWithModuleScript;
type = "text/html";
}
const headers = {
"Content-Length": Buffer.byteLength(body),
"Content-Type": type
};
res.writeHead(200, headers);
res.end(body);
});
app.listen(port, evaluatePageContent);