/
index.js
148 lines (126 loc) · 4.55 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { opendir } from 'fs/promises';
import { resolve, join } from 'path';
import { pathToFileURL } from 'url';
let QUEUE = [],
SUITES = [],
totalPasses = 0,
totalFailures = 0,
totalSkips = 0,
onlySuite = null,
skipSuite = [],
fail = false,
isFn = x => typeof x == 'function',
isNum = Number.isFinite,
start = (now = Date.now()) => _ => ((Date.now() - now) / 1000) + 's',
p = (s = '', ...c) => console.log(c.map(x => `\x1b[${x}m`).join('') + `${s}\x1b[0m`);
function noop() {}
function chain(ctx, ...ops) {
let promise = Promise.resolve();
for (let op of ops) promise = promise.then(_ => op(ctx));
return promise;
}
function race(label, op, timeout) {
let tick;
let clear = _ => clearTimeout(tick);
return Promise.race([
new Promise((_, rej) =>
tick = setTimeout(
_ => rej(`[${label}] timed out after ${timeout}s.`),
1000 * timeout
)
),
isFn(op) ? op() || 1 && clear() : op.finally(clear)
]);
}
function printSuite({ name, errors, passes, failures, skip, time }) {
p(name, 4, 1);
errors.map(e => p('✗ ' + e[0], 47, 30) + console.trace(e[1]) + p());
failures && p(`✗ ${failures} tests failed`, 41);
passes && p(`✓ ${passes} tests passed`, 42);
skip && p(`↷ ${skip} tests skipped`, 30, 43);
p(`\n⧗ ${time}\n`);
}
export async function run({ parallel = false, path = '', ext = /\.test.(js|jsx)$/ } = {}) {
if (path) {
for (let file of await walk(resolve(path))) {
if (ext.test(file)) await import(pathToFileURL(file));
}
}
for (let name in SUITES)
if (!skipSuite.includes(name) && !(onlySuite && name != onlySuite))
QUEUE.push(SUITES[name]);
let end = start();
return (parallel
? Promise.all(QUEUE.map(p => p()))
: (async _ => {
let results = [];
for (let job of QUEUE) results.push(await job());
return results;
})()
).then(results =>
results.map(printSuite)
).finally(_ => {
p(`• • •\n\nPassed: ${totalPasses}\nFailed: ${totalFailures}\nSkipped: ${totalSkips}\n`);
if (skipSuite.length || onlySuite)
p(`↷ ${skipSuite.length || Object.keys(SUITES).length - 1} suites skipped`, 30, 43) + p('');
p(`Duration: ${end()}\n`);
process.exit(fail ? 1 : 0);
});
}
suite.not = suite.skip = name =>
skipSuite.push(name) && suite(name);
suite.only = (name, ...args) =>
(onlySuite = name) && suite(name, ...args);
export function suite(name, { timeout = 1 } = {}) {
let ctx = {},
tests = [],
errors = [],
skip = 0,
passes = 0,
failures = 0,
onlyTest = null,
$ = (label, testcase, x, y) =>
tests.push([label, testcase, x, y]);
$.test = $;
$.before = { each: noop, all: noop };
$.after = { each: noop, all: noop };
$.not = $.skip = _ => skip++;
$.only = (label, testcase, x, y) =>
onlyTest = [label, testcase, x, y];
SUITES[name] = _ => chain().then(async _ => {
let end = start();
await runOp('before.all hook', chain(ctx, $.before.all));
if (onlyTest) skip += tests.length, tests = [onlyTest];
for (let [label, testcase, x, y] of tests)
await runOp(
label,
chain(ctx, $.before.each, testcase),
x,
y,
_ => (passes++, totalPasses++),
_ => failures++,
$.after.each
);
await runOp('after.all hook', chain(ctx, $.after.all));
if (failures) totalFailures += failures, fail = true;
if (skip) totalSkips += skip;
return { name, errors, passes, failures, skip, time: end() };
});
function runOp(label, op, x, y, onSuccess = noop, onFail = noop, onComplete = noop) {
let opTimeout = isNum(x) ? x : (isNum(y) ? y : timeout);
let cleanup = isFn(x) ? x : (isFn(y) ? y : noop);
let addError = e => errors.push([label, e]);
return race(label, op, opTimeout).then(onSuccess)
.catch(e => addError(e) && onFail())
.finally(_ => race(label, chain(ctx, cleanup, onComplete), timeout).catch(addError));
}
return $;
}
async function walk(dir, files = []) {
for await (let dirent of await opendir(dir)) {
const file = join(dir, dirent.name);
if (dirent.isDirectory()) files.concat(await walk(file, files));
else if (dirent.isFile()) files.push(file);
}
return files;
}