-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
169 lines (120 loc) · 4.12 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
var fs = require('fs');
var tapOut = require('tap-out');
var through = require('through2');
var duplexer = require('duplexer');
var format = require('chalk');
var prettyMs = require('pretty-ms');
var _ = require('lodash');
var repeat = require('repeat-string');
var symbols = require('figures');
var lTrimList = require('./lib/utils/l-trim-list');
module.exports = function (spec) {
spec = spec || {};
var OUTPUT_PADDING = spec.padding || ' ';
var output = through();
var parser = tapOut();
var stream = duplexer(parser, output);
var startTime = new Date().getTime();
output.push('\n');
parser.on('test', function (test) {
output.push('\n' + pad(format.underline(test.name)) + '\n\n');
});
// Passing assertions
parser.on('pass', function (assertion) {
if (/# SKIP/.test(assertion.name)) {
var name = assertion.name.replace(' # SKIP', '')
name = format.cyan('- ' + name);
output.push(pad(' ' + name + '\n'));
}
else {
var glyph = format.green(symbols.tick);
var name = format.dim(assertion.name);
output.push(pad(' ' + glyph + ' ' + name + '\n'));
}
});
// Failing assertions
parser.on('fail', function (assertion) {
var glyph = symbols.cross;
var title = glyph + ' ' + assertion.name;
var raw = format.cyan(prettifyRawError(assertion.error.raw));
var divider = _.fill(
new Array((title).length + 1),
'-'
).join('');
output.push('\n' + pad(' ' + format.red(title) + '\n'));
output.push(pad(' ' + format.red(divider) + '\n'));
output.push(raw);
stream.failed = true;
});
parser.on('comment', function (comment) {
output.push(pad(' ' + format.yellow(comment.raw)) + '\n');
});
// All done
parser.on('output', function (results) {
output.push('\n\n');
// Most likely a failure upstream
if (results.plans.length < 1) {
process.exit(1);
}
if (results.fail.length > 0) {
output.push(formatErrors(results));
output.push('\n');
}
output.push(formatTotals(results));
output.push('\n\n\n');
// Exit if no tests run. This is a result of 1 of 2 things:
// 1. No tests and asserts were written
// 2. There was some error before the TAP got to the parser
if (results.tests.length === 0 &&
results.asserts.length === 0) {
process.exit(1);
}
});
// Utils
function prettifyRawError (rawError) {
return rawError.split('\n').map(function (line) {
return pad(line);
}).join('\n') + '\n\n';
}
function formatErrors (results) {
var failCount = results.fail.length;
var past = (failCount === 1) ? 'was' : 'were';
var plural = (failCount === 1) ? 'failure' : 'failures';
var out = '\n' + pad(format.red.bold('Failed Tests:') + ' There ' + past + ' ' + format.red.bold(failCount) + ' ' + plural + '\n');
out += formatFailedAssertions(results);
return out;
}
function formatTotals (results) {
if (results.tests.length === 0 &&
results.asserts.length === 0) {
return pad(format.red(symbols.cross + ' No tests found'));
}
return _.filter([
pad('total: ' + results.asserts.length),
pad(format.green('passing: ' + results.pass.length)),
results.fail.length > 0 ? pad(format.red('failing: ' + results.fail.length)) : undefined,
pad('duration: ' + prettyMs(new Date().getTime() - startTime))
], _.identity).join('\n');
}
function formatFailedAssertions (results) {
var out = '';
var groupedAssertions = _.groupBy(results.fail, function (assertion) {
return assertion.test;
});
_.each(groupedAssertions, function (assertions, testNumber) {
// Wrie failed assertion's test name
var test = _.find(results.tests, {number: parseInt(testNumber)});
out += '\n' + pad(' ' + test.name + '\n\n');
// Write failed assertion
_.each(assertions, function (assertion) {
out += pad(' ' + format.red(symbols.cross) + ' ' + format.red(assertion.name)) + '\n';
});
out += '\n';
});
return out;
}
function pad (str) {
return OUTPUT_PADDING + str;
}
return stream;
};