-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
159 lines (153 loc) · 4.06 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
149
150
151
152
153
154
155
156
157
158
159
const queue = []
exports.test = (label, testFn) => {
queue.push({
label,
fn: testFn
})
}
exports.test.skip = (label, testFn, doSkip = true) => {
queue.push({
label,
fn: doSkip ? Function.prototype : testFn
})
}
exports.test.timeout = (label, testFn, mSec, doTimeout = true, error = new Error(`TimeoutError: ${mSec}ms exceeded.`)) => {
queue.push({
label,
fn: (done) => {
try {
const timeout = setTimeout(doTimeout ? done : Function.prototype, mSec, error)
testFn(err => {
clearTimeout(timeout)
if (!timeout._called || !doTimeout) done(err)
})
if (testFn.length === 0) done(null)
} catch (err) {
done(err)
}
}
})
}
exports.beforeEach = (beforeFn, { assign } = Object) => {
queue.map(context => {
const fn = context.fn
return assign(context, {
fn (done) {
try {
beforeFn()
fn(done) // aint this just another wrap of fn in the end
if (fn.length === 0) done(null)
} catch (err) {
done(err)
}
}
})
})
}
exports.afterEach = (afterFn, { assign } = Object) => {
queue.map(context => {
const fn = context.fn
return assign(context, {
fn (done) {
try {
fn((err) => { // aint this just another wrap of fn in the end
try {
afterFn(() => done(err))
if (afterFn.length === 0) done(err)
} catch (err) {
done(err)
}
})
if (fn.length === 0) {
try {
afterFn(done)
if (afterFn.length === 0) done(null)
} catch (err) {
done(err)
}
}
} catch (err) {
done(err)
}
}
})
})
}
exports.reporter = Object.hasOwn(global, 'reporter')
? global.reporter
: function (context) {
const { elapsed } = timerFor(context)
return {
toString (err) {
const indent = indentFor(context)
if (queue.length > 0) { // context
console.log('%s%s', indent, context.label)
} else { // test
if (err) {
process.exitCode = 1
console.log('%s\x1b[31m✘\x1b[0m %s (%dms)', indent, context.label, elapsed())
if (err.name) {
console.log(' %s\x1b[31m%s\x1b[0m', indent, err.name, err.message)
console.error(err.stack.toString().split('\n').splice(1).join('\n'))
} else {
console.log(' %s%s', indent, err)
}
} else {
if (context.fn === Function.prototype) {
console.log('%s- %s', indent, context.label)
} else {
console.log('%s\x1b[32m✔\x1b[0m %s (%dms)', indent, context.label, elapsed())
}
}
}
}
// summary #1
}
function indentFor (context, length = 0) {
if (context.parent === undefined) {
return Array.from({ length }).fill(' ').join('')
}
return indentFor(context.parent, ++length)
}
function timerFor (context, initial = Date.now()) {
return {
elapsed () {
return Date.now() - initial
}
}
}
}
process.setMaxListeners(0)
process.once('beforeExit', function shift (repoter) {
if (queue.length === 0) return
const { fn, done } = (function ([context, ...pending]) {
const { toString } = exports.reporter(context)
return {
done (err) {
toString(err)
queue.forEach(bindTo(context))
queue.push(...pending)
shift(err)
function bindTo (parent, { assign } = Object) {
return (context) => assign(context, { parent })
}
},
fn: context.fn
}
})(queue)
const handle = trap(done) // move trap to shiftFn
try {
queue.length = 0
fn(handle)
if (fn.length === 0) handle(null)
} catch (err) {
handle(err)
}
function trap (done) {
process.once('uncaughtException', done)
return (err = null) => {
process.removeListener('uncaughtException', done)
done(err)
}
}
})