-
Notifications
You must be signed in to change notification settings - Fork 2
/
tap-nirvana.js
248 lines (191 loc) · 6.74 KB
/
tap-nirvana.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
const fs = require('fs');
const tapOut = require('tap-out');
const through = require('through2');
const duplexer = require('duplexer');
const format = require('chalk');
const prettyMs = require('pretty-ms');
const _ = require('lodash');
const repeat = require('repeat-string');
const symbols = require('figures');
const stringify = require("json-stringify-pretty-compact");
const vdiff = require ("variable-diff");
function lTrimList (lines) {
var leftPadding;
// Get minimum padding count
_.each(lines, function (line) {
var spaceLen = line.match(/^\s+/)[0].length;
if (leftPadding === undefined || spaceLen < leftPadding) {
leftPadding = spaceLen;
}
});
// Strip padding at beginning of line
return _.map(lines, function (line) {
return line.slice(leftPadding);
});
}
/**
* If you try to deepEqual two JSON objects in tape, by the time these reach us
* here, they are stringified Javascript (not JSON!) objects. So we need to
* revive them and the only way is using eval. To make the usage of eval safe
* we wrap it in JSON.stringify and then restore with JSON.parse
*
* @param {*String} jsString
*/
function reviveJSON(jsString) {
let omg;
eval ("omg = JSON.parse(JSON.stringify(" + jsString + "))");
return omg;
}
/**
* Remove lines from error stack that belong to test runner. Nobody cares
* @param {*String} stack
*/
function removeUselessStackLines(stack) {
let pretty = stack.split('\n');
pretty = pretty.filter((line) => {
return !line.includes('node_modules') && !line.includes('Error');
});
pretty = pretty.join('\n');
return pretty;
}
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.cyan(test.name)) + '\n');
});
// Passing assertions
parser.on('pass', function (assertion) {
var glyph = format.green(symbols.tick);
var name = format.dim(assertion.name);
output.push(pad(' ' + glyph + pad(name) + '\n'));
});
// Failing assertions
parser.on('fail', function (assertion) {
var glyph = symbols.cross;
var title = glyph + pad(assertion.name);
var divider = _.fill(
new Array((title).length + 1),
'-'
).join('');
output.push(pad(' ' + format.red(title) + '\n'));
output.push(pad(' ' + format.red(divider) + '\n'));
let skipObjectDiff = true;
let errorMessage = format.magenta("operator:") + " deepEqual\n";
if (assertion.error.operator === 'deepEqual') {
skipObjectDiff = false;
try {
const exObj = reviveJSON(assertion.error.expected);
const acObj = reviveJSON(assertion.error.actual);
const expected = stringify(exObj);
const actual = stringify(acObj);
if (typeof exObj == 'object' && typeof acObj == 'object') {
errorMessage += format.magenta("expected: ") + expected + "\n";
var difference = vdiff(exObj, acObj).text;
errorMessage += format.magenta("diff: ") + difference + "\n";
const moreUsefulStack = removeUselessStackLines(assertion.error.stack);
errorMessage += format.magenta("source: ") + format.gray(moreUsefulStack) + "\n";
} else {
skipObjectDiff = true;
}
} catch (err) {
console.log("error fired " + err);
skipObjectDiff = true;
}
}
if (skipObjectDiff) {
const expected = assertion.error.expected;
const actual = assertion.error.actual;
//errorMessage += format.magenta("expected: ") + expected + "\n";
const delta = vdiff(expected, actual).text;
errorMessage += format.magenta("diff: ") + delta + "\n";
const moreUsefulStack = removeUselessStackLines(assertion.error.stack);
errorMessage += format.magenta("source: ") + format.gray(moreUsefulStack) + "\n";
}
errorMessage = prettifyRawError(errorMessage, 3);
output.push(errorMessage);
stream.failed = true;
});
parser.on('comment', function (comment) {
output.push('\n' + pad(' ' + format.yellow(comment.raw)));
});
// 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');
// Exit if no tests run. This is a result of 1 of 2 things:
// 1. No tests were written
// 2. There was some error before the TAP got to the parser
if (results.tests.length === 0) {
process.exit(1);
}
});
// Utils
function prettifyRawError (rawError, indentIterations=1) {
let pretty = rawError.split('\n');
pretty = pretty.map((line) => {
let padded = line;
for (let i=0; i<= indentIterations; i++) {
padded = pad(padded);
}
return padded;
});
pretty = pretty.join('\n') + '\n';
return pretty;
}
// this duplicates errors that we already showd.
// @TODO : remove
function formatErrors (results) {
return '';
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) {
return pad(format.red(symbols.cross + ' No tests found'));
}
return pad(format.green('passed: ' + results.pass.length + ',')) +
pad(format.red('failed: ' + results.fail.length)) +
pad('of ' + results.asserts.length + ' tests') +
pad(format.dim('(' + prettyMs(new Date().getTime() - startTime) + ')'));
}
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;
};