-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
40 lines (35 loc) · 1.32 KB
/
setup.ts
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
/**
* uvu has no reliable or future-proof way to run and get the test results programmatically yet.
* @see [Improve Programmatic Usage · Issue #113 · lukeed/uvu](https://github.com/lukeed/uvu/issues/113)
*/
/* eslint-disable no-console */
import { onInit, done } from 'wrightplay';
let total: number | undefined;
let passed: number | undefined;
let skipped: number | undefined;
const updateIfFound = (str: string, regex: RegExp, original: number | undefined) => {
const numStr = str.match(regex)?.[1];
return numStr ? Number(numStr) : original;
};
const originalLog = console.log;
console.log = new Proxy(originalLog, {
apply(target, thisArg, argArray: unknown[]) {
const msg = argArray[0];
if (typeof msg === 'string') {
total = updateIfFound(msg, / +Total: +(\d+)/, total);
passed = updateIfFound(msg, / +Passed: +(\d+)/, passed);
skipped = updateIfFound(msg, / +Skipped: +(\d+)/, skipped);
if (total !== undefined && passed !== undefined && skipped !== undefined && / Duration: /.test(msg)) {
console.log = originalLog;
done(total - passed - skipped > 0 ? 1 : 0);
}
}
return target.apply(thisArg, argArray);
},
});
// Proxy console.log before importing uvu.
const uvuImport = import('uvu');
onInit(async () => {
const { test } = await uvuImport;
test.run();
});