-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (54 loc) · 1.56 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
/**
* test function for `bft`
*
* @param {string} description - description of test
* @param {function|Promise<void>} [assertion] - test code
*/
module.exports.test = async (description, assertion = null) => {
const log = (txt) => {
return process.stdout.write(
process.stdout.hasColors && process.stdout.hasColors() === true
? txt
.replace(/^PASSED/, '\x1b[32mPASSED\x1b[0m')
.replace(/^FAILED/, '\x1b[31mFAILED\x1b[0m')
.replace(/^SKIPPED/, '\x1b[33mSKIPPED\x1b[0m')
: txt
);
};
const [, caller] = new Error().stack
.split('\n')
.filter((x) => x.startsWith(' at'));
const [, filename] = caller.match(/\((.*)\)/) || [
,
caller.replace(' at', '').trim(),
];
const origin =
process.stdout.hasColors && process.stdout.hasColors() === true
? `\x1b[2m${filename}\x1b[0m`
: filename;
const name =
process.stdout.hasColors && process.stdout.hasColors() === true
? `\x1b[1m${description}\x1b[0m`
: description;
setImmediate(async () => {
try {
if (assertion === null) {
log(`SKIPPED\t${origin}\t>> ${name}`);
return;
}
const result = assertion();
await result;
log(`PASSED\t${origin}\t>> ${name}`);
} catch (err) {
log(`FAILED\t${origin}\t>> ${name}`);
const lines = err.stack
.split('\n')
.map((x) => `\t${x}`)
.join('\n');
log(`\n${lines}`);
process.exitCode = process.exitCode ? process.exitCode + 1 : 1;
} finally {
log('\n');
}
});
};