-
Notifications
You must be signed in to change notification settings - Fork 3
/
reporter.js
229 lines (214 loc) · 6.84 KB
/
reporter.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
var util = require("util");
require("colors");
// TODO consider piping isTTY through querystring to disable colors if phantom
// results are piped on the Node.js side.
var colors = (
typeof window !== "undefined" || // PhantomJS
typeof process !== "undefined" && // Node.js attached to terminal
typeof process.stdout !== "undefined" &&
process.stdout.isTTY
);
function getStackTrace() {
var stack = new Error("").stack;
if (typeof stack === "string") {
stack = stack.replace(/^[^\n]*\n[^\n]\n/, "");
stack = annotateStackTrace(stack);
}
return stack;
}
function annotateStackTrace(stack) {
if (stack && colors) {
stack = stack.replace(/\n ([^\n]+\-(?:spec|test)\.js[^\n]+)/g, function ($0, $1) {
return ("\n → " + $1).bold;
});
}
return stack;
}
module.exports = Reporter;
function Reporter(options) {
options = options || {};
this.showFails = !!options.showFails;
this.showPasses = !!options.showPasses;
this.root = this;
this.passed = 0;
this.failed = 0;
this.skipped = 0;
this.errors = 0;
this.passedAssertions = 0;
this.failedAssertions = 0;
this.depth = 0;
}
Reporter.prototype.start = function (test) {
var child = Object.create(this);
child.test = test;
child.parent = this;
child.depth = this.depth + 1;
// A test passes if all assertions pass.
// A test that should fail passes if any assertion fails.
// Tests that [should fail] exist only for validating the test runner
// itself and is only necessarily implemented by this test reporter.
child.failed = test.shouldFail;
child.skipped = false;
var message = (Array(child.depth + 1).join("❯") + " " + test.type + " " + test.name + (test.async ? " async".white : ""));
if (test.skip) {
message = message + " (skipped)";
if (colors) {
message = message.cyan;
}
} else {
if (colors) {
message = message.grey;
}
}
child.message = message;
if (!this.showFails) {
console.log(child.message);
}
return child;
};
Reporter.prototype.end = function (test) {
if (this.showFails && this.failed) {
console.log((Array(this.depth + 1).join("↑") + " " + test.type + " " + test.name).grey);
}
if (this.failed && this.parent && this.parent.parent) {
this.parent.failed = true;
}
if (test.type === "it") {
if (this.failed) {
this.root.failed++;
} else if (this.skipped) {
this.root.skipped++;
} else {
this.root.passed++;
}
}
};
Reporter.prototype.skip = function (test) {
this.skipped = true;
this.root.skipped++;
};
Reporter.prototype.assert = function (guard, isNot, messages, objects) {
var passed = !guard === isNot;
if ((!passed && !this.test.shouldFail) || this.showPasses) {
for (var index = 0; index < Math.max(messages.length, objects.length); index++) {
if (index < messages.length) {
var message = "" + messages[index];
if (isNot) {
message = message.replace(/\[not\] /, "not ");
} else {
message = message.replace(/\[not\] /, "");
}
if (colors) {
if (passed !== this.test.shouldFail) {
message = message.green;
} else {
message = message.red;
}
}
console.log(message);
}
if (index < objects.length) {
console.log(util.inspect(objects[index], {colors: colors, depth: null}));
}
}
}
if (!passed && !this.test.shouldFail) {
var stack = getStackTrace();
if (stack) {
console.log(annotateStackTrace(stack));
}
}
if (passed) { // passed
if (this.test.shouldFail) { // but should fail
this.failed = false;
} else { // and passed
this.root.passedAssertions++;
}
} else { // failed
if (!this.test.shouldFail) { // but should pass
this.failed = true;
this.root.failedAssertions++;
} else { // and should fail
this.failed = false;
this.root.passedAssertions++;
}
}
};
Reporter.prototype.error = function (error, test) {
if (this.test.shouldFail) {
this.failed = false;
} else {
console.log(colors ? "error".red : "error");
if (typeof error.stack === "string") {
console.log(annotateStackTrace(error.stack));
} else if (error) {
console.log(error);
}
this.failed = true;
this.root.errors++;
}
};
Reporter.prototype.enter = function () {
if (typeof alert === "undefined") {
var self = this;
this.exitListener = function (code) {
self.failed++;
if (colors) {
console.log("test never completes".red);
} else {
console.log("test never completes");
}
self.exit(code !== 0);
};
process.on("exit", this.exitListener);
}
};
Reporter.prototype.summarize = function (suite) {
if (colors && !this.failed && this.passed) {
console.log((this.passed + " passed tests").green);
} else {
console.log(this.passed + " passed tests");
}
if (colors && !this.failedAssertions && this.passedAssertions && !this.failed && this.passed) {
console.log((this.passedAssertions + " passed assertions").green);
} else {
console.log(this.passedAssertions + " passed assertions");
}
if (colors && this.failed) {
console.log((this.failed + " failed tests").red);
} else {
console.log(this.failed + " failed tests");
}
if (colors && this.failedAssertions) {
console.log((this.failedAssertions + " failed assertions").red);
} else {
console.log(this.failedAssertions + " failed assertions");
}
if (colors && this.errors) {
console.log((this.errors + " errors").red);
} else {
console.log(this.errors + " errors");
}
var skipped = suite.testCount - this.passed - this.failed;
if (colors && skipped) {
console.log((skipped + " skipped tests").cyan);
} else if (skipped) {
console.log(skipped + " skipped tests");
}
};
Reporter.prototype.exit = function (exiting) {
if (typeof alert === "undefined") {
// Node.js
process.removeListener("exit", this.exitListener);
if (!exiting) {
process.exit(this.failed ? -1 : 0);
}
} else {
// PhantomJS
if (this.failed) {
alert("Jasminum tests failed.");
} else {
alert("Jasminum tests completed.");
}
}
};