This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
BootstrapReporter.js
354 lines (281 loc) · 12.4 KB
/
BootstrapReporter.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50, forin: true */
/*global jasmine, $, define, document, require */
define(function (require, exports, module) {
'use strict';
var UrlParams = require("utils/UrlParams").UrlParams;
jasmine.BootstrapReporter = function (doc, filter) {
this.document = doc || document;
this._env = jasmine.getEnv();
this.params = new UrlParams();
this.params.parse();
// parse querystring
var self = this,
i,
p;
this._runAll = this.params.get("spec") === "All";
// _topLevelFilter is applied first - selects Performance vs. Unit test suites
this._topLevelFilter = filter;
// Jasmine's runner uses the specFilter to choose which tests to run.
// If you selected an option other than "All" this will be a subset of all tests loaded.
this._env.specFilter = this.createSpecFilter(this.params.get("spec"));
this._runner = this._env.currentRunner();
// build DOM immediately
var container = $(
'<div class="container-fluid">' +
'<div class="row-fluid">' +
'<div class="span4">' +
'<ul id="suite-list" class="nav nav-pills nav-stacked">' +
'</ul>' +
'</div>' +
'<div id="results-container" class="span8">' +
'</div>' +
'</div>' +
'</div>'
);
$(this.document.body).append(container);
this._topLevelSuiteMap = {};
this.$suiteList = $("#suite-list");
this.$resultsContainer = $("#results-container");
};
/**
* @private
* Filters specs by full name. Applies _topLevelFilter first before checking
* for a matching starting substring.
*/
jasmine.BootstrapReporter.prototype.createSpecFilter = function (filterString) {
var self = this;
return function (spec) {
// filterString is undefined when no top-level suite is active (e.g. "All", "HTMLUtils", etc.)
// When undefined, all specs fail this filter and no tests are ran. This is by design.
// This setup allows the SpecRunner to load initially without automatically running all tests.
if (filterString === undefined) {
return false;
}
if (!self._topLevelFilter(spec)) {
return false;
}
if (filterString === "All") {
return true;
}
if (spec.getFullName() === filterString) {
return true;
}
// spec.getFullName() concatenates the names of all containing describe()s. We want to filter
// on just the outermost suite's name (i.e., the item that was selected in the spec list UI)
// to avoid ambiguity when suite names share the same prefix.
var topLevelSuite = spec.suite;
while (topLevelSuite.parentSuite) {
topLevelSuite = topLevelSuite.parentSuite;
}
return topLevelSuite.description === filterString;
};
};
jasmine.BootstrapReporter.prototype._createSuiteListItem = function (suite, specCount) {
var suiteName = (suite === null) ? "All" : suite.getFullName(),
$badgeAll = $('<span class="badge">' + specCount + "</span>"),
$badgePassed = $('<span class="badge badge-success" style="display:none"/>'),
$badgeFailed = $('<span class="badge badge-important" style="display:none"/>'),
$anchor = $('<a href="?spec=' + encodeURIComponent(suiteName) + '">' + suiteName + '</a>').append($badgeAll).append($badgePassed).append($badgeFailed),
$listItem = $('<li/>').append($anchor),
self = this,
active;
this._topLevelSuiteMap[suiteName] = {
$badgeAll: $badgeAll,
$badgePassed: $badgePassed,
$badgeFailed: $badgeFailed,
$anchor: $anchor,
$listItem: $listItem,
specCount: specCount,
passedCount: 0,
failedCount: 0
};
return $listItem;
};
jasmine.BootstrapReporter.prototype._countSpecs = function (suite) {
var count = 0,
self = this;
// count specs attached directly to this suite
suite.specs().forEach(function (spec, index) {
if (self._topLevelFilter(spec)) {
count++;
}
});
// recursively count child suites
suite.suites().forEach(function (child, index) {
count += self._countSpecs(child);
});
return count;
};
jasmine.BootstrapReporter.prototype._createSuiteList = function () {
var topLevel = [].concat(this._runner.topLevelSuites()),
childSuites,
self = this;
topLevel.sort(function compareFullName(a, b) {
var aName = a.getFullName().toLowerCase(),
bName = b.getFullName().toLowerCase();
if (aName < bName) {
return -1;
} else if (aName > bName) {
return 1;
}
return 0;
});
topLevel.forEach(function (suite, index) {
var count = self._countSpecs(suite);
if (count > 0) {
self.$suiteList.append(self._createSuiteListItem(suite, count));
}
});
// count all speces
var allSpecsCount = 0;
$.each(this._topLevelSuiteMap, function (index, value) {
allSpecsCount += value.specCount;
});
// add an "all" top-level suite
this.$suiteList.prepend(this._createSuiteListItem(null, allSpecsCount));
};
jasmine.BootstrapReporter.prototype._showProgressBar = function (spec) {
if (!this.$progressBar) {
this.$progress = $('<div class="bar"/>');
this.$progressBar = $('<div class="progress progress-striped"/>').append(this.$progress);
}
this.$resultsContainer.append(this.$progressBar);
};
jasmine.BootstrapReporter.prototype.reportRunnerStarting = function (runner) {
var specs = runner.specs(),
topLevelData,
self = this;
// create top level suite list navigation
this._createSuiteList();
// highlight the current suite
topLevelData = (this.params.get("spec")) ? this._topLevelSuiteMap[this.params.get("spec")] : null;
if (topLevelData) {
topLevelData.$listItem.toggleClass("active", true);
}
this._specCount = 0;
this._specCompleteCount = 0;
specs.forEach(function (spec, index) {
if (self._env.specFilter(spec)) {
self._specCount++;
}
});
if (this._specCount) {
this._showProgressBar();
// display current running test
this.$info = $('<div class="alert alert-info"/>');
this.$resultsContainer.append(this.$info);
this.$resultsContainer.append($('<hr/>'));
}
};
jasmine.BootstrapReporter.prototype.reportRunnerResults = function (runner) {
if (this.$info) {
this.$info.toggleClass("alert-info", false);
if (runner.results().passed()) {
this.$info.toggleClass("alert-success", true).text("Complete. No failures.");
} else {
this.$info.toggleClass("alert-error", true).text("Complete. See failures.");
}
}
};
jasmine.BootstrapReporter.prototype.reportSuiteResults = function (suite) {
var results = suite.results(),
passed,
data = this._topLevelSuiteMap[suite.getFullName()];
if ((suite.getFullName() === this.params.get("spec")) && data) {
passed = results.passed();
data.$badgeAll.hide();
}
};
jasmine.BootstrapReporter.prototype.reportSpecStarting = function (spec) {
this.$info.text("Running " + spec.getFullName());
};
jasmine.BootstrapReporter.prototype._getTopLevelSuiteData = function (spec) {
var topLevelSuite = spec.suite;
while (topLevelSuite.parentSuite) {
topLevelSuite = topLevelSuite.parentSuite;
}
return this._topLevelSuiteMap[topLevelSuite.getFullName()];
};
jasmine.BootstrapReporter.prototype._updateSuiteStatus = function (data, results) {
// update pass/fail totals
if (results.passed()) {
data.passedCount++;
} else {
data.failedCount++;
}
// update status badges
if (data.passedCount) {
data.$badgePassed.show().text(data.passedCount);
} else {
data.$badgePassed.hide();
}
if (data.failedCount) {
data.$badgeFailed.show().text(data.failedCount);
} else {
data.$badgeFailed.hide();
}
var specsRemaining = data.specCount - data.passedCount - data.failedCount;
if (specsRemaining === 0) {
data.$badgeAll.hide();
} else {
data.$badgeAll.text(specsRemaining);
}
};
jasmine.BootstrapReporter.prototype._updateStatus = function (spec) {
var data = this._getTopLevelSuiteData(spec),
allData,
results;
// Top-level suite data will not exist if filtered
if (!data) {
return;
}
allData = this._topLevelSuiteMap.All;
results = spec.results();
this._updateSuiteStatus(data, results);
this._updateSuiteStatus(allData, results);
};
// Jasmine calls this function for all specs, not just filtered specs.
jasmine.BootstrapReporter.prototype.reportSpecResults = function (spec) {
var results = spec.results(),
$specLink,
$resultDisplay;
if (!results.skipped) {
this._updateStatus(spec);
// update progress
this._specCompleteCount++;
this.$progress.css("width", Math.round((this._specCompleteCount / this._specCount) * 100) + "%");
if (!results.passed()) {
// print suite name if not present
var $suiteHeader = $("#suite-results-" + spec.suite.id);
if ($suiteHeader.length === 0) {
this.$resultsContainer.append($('<div id="suite-results-' + spec.suite.id + '" class="alert alert-info"/>').text(spec.suite.getFullName()));
}
// print spec name
$specLink = $('<a href="?spec=' + encodeURIComponent(spec.getFullName()) + '"/>').text(spec.description);
$resultDisplay = $('<div class="alert alert-error"/>').append($specLink);
// print failure details
var resultItems = results.getItems(),
$message,
i;
for (i = 0; i < resultItems.length; i++) {
var result = resultItems[i];
$message = null;
if (result.type === 'log') {
$message = $('<pre/>').text(result.toString());
} else if (result.type === 'expect' && result.passed && !result.passed()) {
$message = $('<pre/>').text(result.message);
if (result.trace.stack) {
$message = $('<pre/>').text(result.trace.stack);
}
}
if ($message) {
$resultDisplay.append($message);
}
}
this.$resultsContainer.append($resultDisplay);
}
}
};
jasmine.BootstrapReporter.prototype.log = function (str) {
};
});