-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_tests.js
86 lines (74 loc) · 2.12 KB
/
run_tests.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
const { exec, spawn } = require("node:child_process");
const path = require("node:path");
const fetch = require("node-fetch");
const urlJoin = require("url-join");
const PORT = +(process.env.PORT | "9000");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitForServerStart(serverAddress) {
for (let i = 0; i < 25; i++) {
console.log("Trying to connect to the server...");
try {
// Fetch react-dom@18.1.0
await fetch(
urlJoin(serverAddress, "/package/MyhyZWFjdC1kb21AMTguMS4wKQ=="),
{}
);
return true;
} catch (err) {
console.error(err);
}
console.log("Waiting 50ms before retrying...");
await sleep(50);
}
throw new Error("Server did not start in time :(");
}
async function run() {
const buildFile = path.join(__dirname, "/target/release/sandpack-cdn");
const spawnedServer = spawn(buildFile, {
env: {
...process.env,
PORT: PORT,
},
});
spawnedServer.on("spawn", () => {
console.log("Started the server process", buildFile);
});
spawnedServer.on("error", (err) => console.error(err));
spawnedServer.on("disconnect", () => console.log("Server bus disconnected"));
spawnedServer.on("close", () => console.log("Server closed"));
spawnedServer.stdout.setEncoding("utf-8");
spawnedServer.stdout.on("data", (data) => {
console.log(data);
});
spawnedServer.stderr.setEncoding("utf-8");
spawnedServer.stderr.on("data", (data) => {
console.error(data);
});
const cdnAddress = `http://localhost:${PORT}`;
await waitForServerStart(cdnAddress);
console.log("Server has responded successfully");
const spawnedTest = spawn("jest", ["--forceExit"], {
stdio: "inherit",
env: {
...process.env,
CDN_ROOT: cdnAddress,
},
});
await new Promise((resolve, reject) => {
spawnedTest.on("close", (code) => {
if (code !== 0) {
reject(new Error("tests failed"));
} else {
resolve();
}
});
}).finally(() => {
spawnedServer.kill();
});
}
run().catch((err) => {
process.exitCode = 1;
console.error(err);
});