-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
72 lines (60 loc) · 2.75 KB
/
test.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
// using NodeJS' assert library to test flug
const assert = require("node:assert");
const child_process = require("node:child_process");
const fs = require("node:fs");
const $ = str => {
try {
const output = child_process
.execSync(str + " 2>&1", {
cwd: __dirname,
stdio: [0, fs.openSync("log.out", "w"), fs.openSync("err.out", "w")]
})
.toString();
return output;
} catch (error) {
const msg = fs.readFileSync("log.out", "utf-8");
return msg;
}
};
const t = (cmd, expected) => {
const out = $(cmd);
if (expected instanceof RegExp) {
if (out.match(expected)) {
console.log("\x1B[32msuccessfully\x1B[0m ran " + cmd);
} else {
console.log("out:", [out]);
throw new Error("failed");
}
} else {
try {
assert.strictEqual(out, expected);
console.log("\x1B[32msuccessfully\x1B[0m ran " + cmd);
} catch (error) {
console.log("out:", [out]);
throw error;
}
}
};
t("node ./examples/test.addition.js", "\x1B[32msuccess: addition\x1B[0m\n");
t("TEST_NAME=' addition ' node ./examples/test.addition.js", "\x1B[32msuccess: addition\x1B[0m\n");
t("node ./examples/test.error.js", "\n\x1B[31mfailed: error\x1B[0m\nerror\n\n\n");
const cmd = "node ./examples/test.failure.js";
const log = $(cmd).trim().replace(/\n/g, "");
assert.strictEqual(log.includes("failed: failure"), true); // includes failure message with test name
assert.strictEqual(log.includes("at "), true); // include stack trace
assert.strictEqual(log.includes("test.failure.js"), true);
console.log("\x1B[32msuccessfully\x1B[0m ran " + cmd);
t("node ./examples/test.timed.js", /\x1B\[32msuccess \((\d)ms\)\: timed\x1B\[0m\n/);
t("node ./examples/test.queue.js", "\x1B[32msuccess: first\x1B[0m\n\x1B[32msuccess: second\x1B[0m\n\x1B[32msuccess: third\x1B[0m\n");
t("node ./examples/test.filename.js", "\x1B[33mskipped: invalid filename\x1B[39m\n\x1B[32msuccess: valid filename\x1B[0m\n");
t("node ./examples/test.dir.js", "\x1B[33mskipped: invalid dir\x1B[39m\n\x1B[32msuccess: valid dir\x1B[0m\n");
t("node ./examples/test.name.js", "\x1B[33mskipped: skip\x1B[39m\n\x1B[32msuccess: only\x1B[0m\n");
t("node ./examples/test.name-wildcard.js", "\x1B[33mskipped: skip\x1B[39m\n\x1B[32msuccess: only[this]\x1B[0m\n");
const start_test_gap = performance.now();
t("node ./examples/test.gap.js", "\x1B[32msuccess: first\x1B[0m\n\x1B[32msuccess: second\x1B[0m\n");
const end_test_gap = performance.now();
const duration_test_gap = Math.round((end_test_gap - start_test_gap) / 1000);
assert.strictEqual(duration_test_gap >= 3 && duration_test_gap <= 5, true);
const cmd2 = "node ./examples/test.only-one-arg.js";
const log2 = $(cmd2).trim().replace(/\n/g, "");
assert.strictEqual(log2.includes("you only supplied one argument"), true);