-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
108 lines (95 loc) · 2.23 KB
/
index.mjs
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
// @ts-check
import { spawn } from "node:child_process";
import * as path from "node:path";
import * as fs from "node:fs";
import { fileURLToPath } from "node:url";
import { chromium } from "playwright";
import treeKill from "tree-kill";
import { customAssertMap, verify } from "./verify.mjs";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const appDir = path.resolve(__dirname, "../../apps");
const list = fs.readdirSync(appDir);
const urlRegex = /http:\/\/(?:www\.)?[a-zA-Z0-9-]+\:\d+/g
// ignore app list
const ignoreList = ["qwik-jsx", "qwik-tsx"];
const includeList = process.argv.slice(2);
let hasError = false;
for (let appName of list) {
if (ignoreList.includes(appName)) {
console.warn(`Ignoring ${appName}`);
continue;
}
if (includeList.length > 0 && !includeList.includes(appName)) {
console.warn(`Ignoring ${appName} since it is not included`);
continue;
}
let abPath = path.resolve(appDir, appName);
let err;
try {
await runInApp(abPath, appName);
} catch (e) {
err = e;
hasError = true;
} finally {
if (err) {
console.error(`Error: ${err} when executed ${appName}`, err);
} else {
console.log(`Case-${appName} : Success`);
}
}
}
if (hasError) {
process.exit(-1);
}
/**
* @param {string} dirPath
* @param {string} caseName
*
* */
function runInApp(dirPath, caseName) {
return new Promise((resolve, reject) => {
const p = spawn("npm", ["run", "preview"], {
stdio: "inherit",
cwd: dirPath,
});
let data = "";
setTimeout(() => {
runTest()
}, 2000)
async function runTest() {
let exitCode = p.exitCode;
let err;
try {
await testInBrowser();
} catch (e) {
err = e;
exitCode = -1;
} finally {
cleanUp(exitCode, () => {
if (err) {
reject(err);
} else {
resolve(undefined);
}
});
}
}
async function testInBrowser() {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto("http://localhost:4173/");
try {
await verify(page, customAssertMap[caseName]);
} catch (err) {
throw new Error(err);
} finally {
await browser.close();
}
// Teardown
}
function cleanUp(exitCode, cb) {
// @ts-ignore
treeKill(p.pid, cb);
}
});
}