-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
155 lines (127 loc) · 3.68 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
let tapOut = require("tap-out");
let through = require("through2");
let duplexer = require("duplexer");
let format = require("chalk");
let prettyMs = require("pretty-ms");
let symbols = require("figures");
module.exports = () =>
{
let output = through();
let parser = tapOut();
let stream = duplexer(parser, output);
let 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) {
// let glyph = format.green(symbols.tick);
// let name = format.dim(assertion.name);
// output.push(pad(" " + glyph + " " + name + "\n"));
//});
// Failing assertions
parser.on("fail", (assertion) =>
{
let glyph = symbols.cross;
let title = glyph + " " + assertion.name;
let raw = format.cyan(prettifyRawError(assertion.error.raw));
let divider = new Array(title.length + 1).fill(
"-"
).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", (comment) =>
{
output.push(pad(" " + format.yellow(comment.raw)) + "\n");
});
// All done
parser.on("output", (results) =>
{
output.push("\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");
// 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)
{
return rawError.split("\n").map((line) =>
{
return pad(line);
}).join("\n") + "\n\n";
}
function formatErrors (results)
{
let failCount = results.fail.length;
let past = (failCount === 1) ? "was" : "were";
let plural = (failCount === 1) ? "failure" : "failures";
let 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"));
}
const temp = [
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))
];
return temp.join("\n");
}
function formatFailedAssertions (results)
{
let out = "";
let groupedAssertions = results.fail.filter((assertion) =>
{
return assertion.test;
});
groupedAssertions.forEach((assertions, testNumber) =>
{
// Wrie failed assertion"s test name
let test = results
.tests.find(results.tests, {number: parseInt(testNumber)});
out += "\n" + pad(" " + test.name + "\n\n");
// Write failed assertion
assertions.forEach((assertion) =>
{
out += pad(" " + format.red(symbols.cross) + " "
+ format.red(assertion.name)) + "\n";
});
out += "\n";
});
return out;
}
function pad (str)
{
return " " + str;
}
return stream;
};