/
baretest.js
60 lines (44 loc) · 1.11 KB
/
baretest.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
const rgb = require('barecolor')
module.exports = function(headline) {
const suite = [],
before = [],
after = [],
only = []
function self(name, fn) {
suite.push({ name: name, fn: fn })
}
self.only = function(name, fn) {
only.push({ name: name, fn: fn })
}
self.before = function(fn) { before.push(fn) }
self.after = function(fn) { after.push(fn) }
self.skip = function(fn) {}
self.run = async function() {
const tests = only[0] ? only : suite
rgb.cyan(headline + ' ')
for (const test of tests) {
try {
for (const fn of before) await fn()
await test.fn()
rgb.gray('• ')
} catch(e) {
for (const fn of after) await fn()
rgb.red(`\n\n! ${test.name} \n\n`)
prettyError(e)
return false
}
}
for (const fn of after) await fn()
rgb.greenln(`✓ ${ tests.length }`)
console.info('\n')
return true
}
return self
}
function prettyError(e) {
const msg = e.stack
if (!msg) return rgb.yellow(e)
const i = msg.indexOf('\n')
rgb.yellowln(msg.slice(0, i))
rgb.gray(msg.slice(i))
}