-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
350 lines (275 loc) · 7.76 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'use strict';
/**
* Module dependencies.
*/
var mocha = require('mocha')
var chalk = require('chalk')
var stripAnsi = require('strip-ansi')
var formatError = require('format-error').format;
var Base = mocha.reporters.Base
var inherits = mocha.utils.inherits
var color = Base.color
/**
* Expose `Spec`.
*/
exports = module.exports = Spec
/**
* Optional hook function; this will
* wrap Mocha's standard functions into
* a wrapper that will print the file and
* line number at which the test can be found.
*/
function getFilenameAndLine() {
var error = new Error()
return error.stack
.split('\n')
.slice(3, 4)
.pop()
.split(process.cwd())
.pop()
.slice(1, -1)
}
function wrap(realFunc) {
return function (label, func) {
var line = getFilenameAndLine()
realFunc(label, function () {
console.log(chalk.cyan(line))
return func.call(this)
})
}
}
var isHookActive = false
exports.hook = function () {
isHookActive = true
global.it = wrap(global.it)
global.it.only = wrap(global.it.only)
global.before = wrap(global.before)
global.beforeEach = wrap(global.beforeEach)
global.after = wrap(global.after)
global.afterEach = wrap(global.afterEach)
};
/**
* Initialize a new `Spec` test reporter.
*
* @param {Runner} runner
*/
function Spec(runner) {
Base.call(this, runner)
if (process.platform === 'win32') {
Base.symbols.ok = '@'
Base.symbols.pending = '?'
} else {
Base.symbols.ok = '✔'
Base.symbols.pending = '⌗'
}
var indents = 0
var n = 0
function indent() {
return Array(indents).join(' ')
}
// On windows, make sure that console writes to stdout
// Note that the global console is NOT replacable; this is
// why we need to replace all functions and attributes manually.
// A hack, for sure.
const c = new console.Console(process.stdout)
for (let attr in console) {
console[attr] = c[attr]
}
let oldStdout = process.stdout.write
let oldStderr = process.stderr.write
var interceptedOutput = []
function createIntercept() {
process.stdout.write = process.stderr.write = function (data) {
interceptedOutput.push(data)
}
}
function unhookIntercept() {
process.stdout.write = oldStdout.bind(process.stdout)
process.stderr.write = oldStderr.bind(process.stderr)
}
function clearLogStack() {
interceptedOutput = []
}
function flushLogStack(indent) {
if (interceptedOutput.length === 0) {
return
}
var stripped = stripAnsi(interceptedOutput.join(''))
if (stripped === '') {
return clearLogStack()
}
var filenameLine = isHookActive
var firstLine = true
interceptedOutput.forEach(function (data) {
let arr = data.toString().split('\n')
arr.pop()
// First line should always be the file name
// printed by the hook
if (filenameLine) {
filenameLine = false
console.log(indent + arr.shift())
}
// Add a space if more logs are to come
if (firstLine) {
firstLine = false
if (interceptedOutput.length > 1 || arr.length > 0) {
console.log()
}
}
arr.forEach(function (line) {
console.log(indent + chalk.blue('>>> logs: ') + line)
})
})
console.log()
clearLogStack()
}
function flushLogStackIfDebug(indent) {
if (process.env.DEBUG) {
flushLogStack(indent)
} else {
clearLogStack()
}
}
function flushIfDebugAndResetIntercept(indent) {
unhookIntercept()
flushLogStackIfDebug(indent)
createIntercept()
}
function humanizeMs(ms) {
if (ms < 1000) {
return ms + (ms === 1 ? ' millisecond' : ' milliseconds')
}
const date = new Date(ms)
const hours = date.getUTCHours()
const minutes = date.getUTCMinutes()
const seconds = date.getUTCSeconds()
let ret = ''
if (hours) {
ret += hours + (hours === 1 ? ' hour ' : ' hours ')
}
if (minutes || hours > 0) {
ret += minutes + (minutes === 1 ? ' minute ' : ' minutes ')
}
ret += seconds + (seconds === 1 ? ' second ' : ' seconds ')
return ret
}
runner.on('start', function () {
console.log(chalk.reset(' '))
createIntercept()
})
runner.on('suite', function (suite) {
if (!suite.title) {
return;
}
unhookIntercept()
indents += 1
flushLogStackIfDebug(indent())
let colorize = chalk.magenta
// For some reason, windows doesn't like magenta
if (process.platform === 'win32') {
colorize = colorize.bold
}
console.log(colorize('%s[%s]') + chalk.reset(' '), indent(), suite.title)
createIntercept()
})
// We use this to add a spacing line between test blocks
// for readability
let someTestsRan = false
runner.on('suite end', function () {
unhookIntercept()
flushLogStackIfDebug(indent())
indents -= 1
if (someTestsRan) {
console.log()
}
someTestsRan = false
createIntercept()
})
runner.on('test', function () {
someTestsRan = true
flushIfDebugAndResetIntercept(indent())
})
runner.on('pending', function (test) {
someTestsRan = true
unhookIntercept()
var fmt = indent()
+ chalk.yellow(' ' + Base.symbols.pending)
+ color('pending', ' %s')
console.log(fmt, test.title)
createIntercept()
})
runner.on('pass', function (test) {
unhookIntercept()
var fmt
if (test.speed === 'fast') {
fmt = indent()
+ color('checkmark', ' ' + Base.symbols.ok)
+ color('pass', ' %s')
console.log(fmt, test.title)
} else {
fmt = indent()
+ color('checkmark', ' ' + Base.symbols.ok)
+ color('pass', ' %s')
+ color(test.speed, ' (%dms)')
console.log(fmt, test.title, test.duration)
}
flushLogStackIfDebug(indent() + ' ')
createIntercept()
})
runner.on('fail', function (test) {
// Delete powerassert-context if present
if (test.err.powerAssertContext) {
delete test.err.powerAssertContext
}
// Needed to append error at the bottom
// of the stack we will be printing out
let output = formatError(test.err)
process.stdout.write(output + '\n')
unhookIntercept()
console.log()
console.log(indent() + color('fail', ' %d) %s'), ++n, test.title)
flushLogStack(indent() + ' ', 'reset')
createIntercept()
})
runner.on('end', function () {
let fmt
let stats = this.stats
let barColor = !stats.failures && 'green' || 'red'
let topLine, bottomLine
if (process.platform === 'win32') {
topLine = '=============================================='
bottomLine = '=============================================='
} else {
topLine = '▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀'
bottomLine = '▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄'
}
unhookIntercept()
flushLogStack('')
console.log(chalk[barColor].bold(topLine))
// passes
fmt = chalk.bold(color('green', ' %d'))
+ color('green', ' passing')
console.log(fmt, stats.passes || 0)
// pending
if (stats.pending) {
fmt = chalk.bold(color('pending', ' %d'))
+ color('pending', ' pending')
console.log(fmt, stats.pending)
}
// failures
if (stats.failures) {
fmt = chalk.bold(color('fail', ' %d'))
+ color('fail', ' failing')
console.log(fmt, stats.failures)
}
fmt = chalk.gray(' Took ')
+ chalk.magenta.bold('%s')
console.log(fmt, humanizeMs(stats.duration))
console.log(chalk[barColor].bold(bottomLine))
console.log()
})
}
/**
* Inherit from `Base.prototype`.
*/
inherits(Spec, Base)