forked from jsdoc/jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.js
292 lines (242 loc) · 10 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
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
module.exports = function(jasmine) {
var util = require('util');
var jasmineNode = {};
//
// Helpers
//
function noop() {
}
jasmineNode.ANSIColors = {
pass : function() {
return '\033[32m';
}, // Green
fail : function() {
return '\033[31m';
}, // Red
neutral : function() {
return '\033[0m';
} // Normal
};
jasmineNode.NoColors = {
pass : function() {
return '';
},
fail : function() {
return '';
},
neutral : function() {
return '';
}
};
jasmineNode.TerminalReporter = function(config) {
this.print_ = config.print || function (str) { process.stdout.write(util.format(str)); };
this.color_ = config.color ? jasmineNode.ANSIColors : jasmineNode.NoColors;
this.started_ = false;
this.finished_ = false;
this.callback_ = config.onComplete || false;
this.suites_ = [];
this.specResults_ = {};
this.failures_ = {};
this.failures_.length = 0;
};
jasmineNode.TerminalReporter.prototype = {
reportRunnerStarting : function(runner) {
this.started_ = true;
this.startedAt = new Date();
var suites = runner.topLevelSuites();
for ( var i = 0; i < suites.length; i++) {
var suite = suites[i];
this.suites_.push(this.summarize_(suite));
}
},
summarize_ : function(suiteOrSpec) {
var isSuite = suiteOrSpec instanceof jasmine.Suite;
// We could use a separate object for suite and spec
var summary = {
id : suiteOrSpec.id,
name : suiteOrSpec.description,
type : isSuite ? 'suite' : 'spec',
suiteNestingLevel : 0,
children : []
};
if (isSuite) {
var calculateNestingLevel = function(examinedSuite) {
var nestingLevel = 0;
while (examinedSuite.parentSuite !== null) {
nestingLevel += 1;
examinedSuite = examinedSuite.parentSuite;
}
return nestingLevel;
};
summary.suiteNestingLevel = calculateNestingLevel(suiteOrSpec);
var children = suiteOrSpec.children();
for ( var i = 0; i < children.length; i++) {
summary.children.push(this.summarize_(children[i]));
}
}
return summary;
},
// This is heavily influenced by Jasmine's Html/Trivial Reporter
reportRunnerResults : function(runner) {
this.reportFailures_();
var results = runner.results();
var resultColor = (results.failedCount > 0) ? this.color_.fail() : this.color_.pass();
var specs = runner.specs();
var specCount = specs.length;
var message = "\n\nFinished in " +
((new Date().getTime() - this.startedAt.getTime()) / 1000) +
" seconds";
this.printLine_(message);
// This is what jasmine-html.js has
// message = "" + specCount + " spec" + ( specCount === 1 ? "" : "s") + ", " + results.failedCount + " failure" + ((results.failedCount === 1) ? "" : "s");
this.printLine_(this.stringWithColor_(this.printRunnerResults_(runner), resultColor));
this.finished_ = true;
if (this.callback_) {
this.callback_(runner);
}
},
reportFailures_ : function() {
if (this.failures_.length === 0) {
return;
}
var indent = ' ', failure, failures;
this.printLine_('\n');
this.print_('Failures:');
for ( var suite in this.failures_) {
if (this.failures_.hasOwnProperty(suite) && suite !== "length") {
this.printLine_('\n');
this.printLine_(suite);
failures = this.failures_[suite];
for ( var i = 0; i < failures.length; i++) {
failure = failures[i];
this.printLine_('\n');
this.printLine_(indent + (i + 1) + ') ' + failure.spec);
this.printLine_(indent + 'Message:');
this.printLine_(indent + indent + this.stringWithColor_(failure.message, this.color_.fail()));
this.printLine_(indent + 'Stacktrace:');
this.print_(indent + indent + failure.stackTrace);
}
}
}
},
reportSuiteResults : function(suite) {
// Not used in this context
},
reportSpecResults : function(spec) {
var result = spec.results();
var msg = '';
if (result.skipped) {
msg = this.stringWithColor_('-', this.color_.ignore());
} else if (result.passed()) {
msg = this.stringWithColor_('.', this.color_.pass());
} else {
msg = this.stringWithColor_('F', this.color_.fail());
this.addFailureToFailures_(spec);
}
this.spec_results += msg;
this.print_(msg);
},
addFailureToFailures_ : function(spec) {
var result = spec.results();
var failureItem = null;
var suite = spec.suite.getFullName();
var failures = null;
var items_length = result.items_.length;
for ( var i = 0; i < items_length; i++) {
if (result.items_[i].passed_ === false) {
failureItem = result.items_[i];
var failure = {
spec : spec.description,
message : failureItem.message,
stackTrace : failureItem.trace.stack
};
failures = this.failures_[suite];
if (!failures) {
this.failures_[suite] = [];
}
this.failures_[suite].push(failure);
this.failures_.length++;
}
}
},
printRunnerResults_ : function(runner) {
var results = runner.results();
var specs = runner.specs();
var msg = '';
msg += specs.length + ' test' + ((specs.length === 1) ? '' : 's') + ', ';
msg += results.totalCount + ' assertion' + ((results.totalCount === 1) ? '' : 's') + ', ';
msg += results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\n';
return msg;
},
// Helper Methods //
stringWithColor_ : function(stringValue, color) {
return (color || this.color_.neutral()) + stringValue + this.color_.neutral();
},
printLine_ : function(stringValue) {
this.print_(stringValue);
this.print_('\n');
}
};
// ***************************************************************
// TerminalVerboseReporter uses the TerminalReporter's constructor
// ***************************************************************
jasmineNode.TerminalVerboseReporter = function(config) {
jasmineNode.TerminalReporter.call(this, config);
// The extra field in this object
this.indent_ = 0;
};
jasmineNode.TerminalVerboseReporter.prototype = {
reportSpecResults : function(spec) {
if (spec.results().failedCount > 0) {
this.addFailureToFailures_(spec);
}
this.specResults_[spec.id] = {
messages : spec.results().getItems(),
result : spec.results().failedCount > 0 ? 'failed' : 'passed'
};
},
reportRunnerResults : function(runner) {
var messages = new Array();
this.buildMessagesFromResults_(messages, this.suites_);
var messages_length = messages.length;
for ( var i = 0; i < messages_length - 1; i++) {
this.printLine_(messages[i]);
}
this.print_(messages[messages_length - 1]);
// Call the parent object's method
jasmineNode.TerminalReporter.prototype.reportRunnerResults.call(this, runner);
},
buildMessagesFromResults_ : function(messages, results) {
var element, specResult, specIndentSpaces, msg = '';
var results_length = results.length;
for ( var i = 0; i < results_length; i++) {
element = results[i];
if (element.type === 'spec') {
specResult = this.specResults_[element.id.toString()];
specIndentSpaces = this.indent_ + 2;
if (specResult.result === 'passed') {
msg = this.stringWithColor_(this.indentMessage_(element.name, specIndentSpaces), this.color_.pass());
} else {
msg = this.stringWithColor_(this.indentMessage_(element.name, specIndentSpaces), this.color_.fail());
}
messages.push(msg);
} else {
this.indent_ = element.suiteNestingLevel * 2;
messages.push('');
messages.push(this.indentMessage_(element.name,this.indent_));
}
this.buildMessagesFromResults_(messages, element.children);
}
},
indentMessage_ : function(message, indentCount) {
var _indent = '';
for ( var i = 0; i < indentCount; i++) {
_indent += ' ';
}
return (_indent + message);
}
};
// Inherit from TerminalReporter
jasmineNode.TerminalVerboseReporter.prototype.__proto__ = jasmineNode.TerminalReporter.prototype;
return jasmineNode;
};