jasmine-jsreporter.js
12.9 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
This file is part of the Jasmine JSReporter project from Ivan De Marino.
Copyright (C) 2011-2014 Ivan De Marino <http://ivandemarino.me>
Copyright (C) 2014 Alex Treppass <http://alextreppass.co.uk>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL IVAN DE MARINO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function (jasmine) {
if (!jasmine) {
throw new Error("[Jasmine JSReporter] 'Jasmine' library not found");
}
// ------------------------------------------------------------------------
// Jasmine JSReporter for Jasmine 1.x
// ------------------------------------------------------------------------
/**
* Calculate elapsed time, in Seconds.
* @param startMs Start time in Milliseconds
* @param finishMs Finish time in Milliseconds
* @return Elapsed time in Seconds */
function elapsedSec(startMs, finishMs) {
return (finishMs - startMs) / 1000;
}
/**
* Round an amount to the given number of Digits.
* If no number of digits is given, than '2' is assumed.
* @param amount Amount to round
* @param numOfDecDigits Number of Digits to round to. Default value is '2'.
* @return Rounded amount */
function round(amount, numOfDecDigits) {
numOfDecDigits = numOfDecDigits || 2;
return Math.round(amount * Math.pow(10, numOfDecDigits)) / Math.pow(10, numOfDecDigits);
}
/**
* Create a new array which contains only the failed items.
* @param items Items which will be filtered
* @returns {Array} of failed items */
function failures(items) {
var fs = [], i, v;
for (i = 0; i < items.length; i += 1) {
v = items[i];
if (!v.passed_) {
fs.push(v);
}
}
return fs;
}
/**
* Collect information about a Suite, recursively, and return a JSON result.
* @param suite The Jasmine Suite to get data from
*/
function getSuiteData(suite) {
var suiteData = {
description : suite.description,
durationSec : 0,
specs: [],
suites: [],
passed: true
},
specs = suite.specs(),
suites = suite.suites(),
i, ilen;
// Loop over all the Suite's Specs
for (i = 0, ilen = specs.length; i < ilen; ++i) {
suiteData.specs[i] = {
description : specs[i].description,
durationSec : specs[i].durationSec,
passed : specs[i].results().passedCount === specs[i].results().totalCount,
skipped : specs[i].results().skipped,
passedCount : specs[i].results().passedCount,
failedCount : specs[i].results().failedCount,
totalCount : specs[i].results().totalCount,
failures: failures(specs[i].results().getItems())
};
suiteData.passed = !suiteData.specs[i].passed ? false : suiteData.passed;
suiteData.durationSec += suiteData.specs[i].durationSec;
}
// Loop over all the Suite's sub-Suites
for (i = 0, ilen = suites.length; i < ilen; ++i) {
suiteData.suites[i] = getSuiteData(suites[i]); //< recursive population
suiteData.passed = !suiteData.suites[i].passed ? false : suiteData.passed;
suiteData.durationSec += suiteData.suites[i].durationSec;
}
// Rounding duration numbers to 3 decimal digits
suiteData.durationSec = round(suiteData.durationSec, 4);
return suiteData;
}
var JSReporter = function () {
};
JSReporter.prototype = {
reportRunnerStarting: function (runner) {
// Nothing to do
},
reportSpecStarting: function (spec) {
// Start timing this spec
spec.startedAt = new Date();
},
reportSpecResults: function (spec) {
// Finish timing this spec and calculate duration/delta (in sec)
spec.finishedAt = new Date();
// If the spec was skipped, reportSpecStarting is never called and spec.startedAt is undefined
spec.durationSec = spec.startedAt ? elapsedSec(spec.startedAt.getTime(), spec.finishedAt.getTime()) : 0;
},
reportSuiteResults: function (suite) {
// Nothing to do
},
reportRunnerResults: function (runner) {
var suites = runner.suites(),
i, j, ilen;
// Attach results to the "jasmine" object to make those results easy to scrap/find
jasmine.runnerResults = {
suites: [],
durationSec : 0,
passed : true
};
// Loop over all the Suites
for (i = 0, ilen = suites.length, j = 0; i < ilen; ++i) {
if (suites[i].parentSuite === null) {
jasmine.runnerResults.suites[j] = getSuiteData(suites[i]);
// If 1 suite fails, the whole runner fails
jasmine.runnerResults.passed = !jasmine.runnerResults.suites[j].passed ? false : jasmine.runnerResults.passed;
// Add up all the durations
jasmine.runnerResults.durationSec += jasmine.runnerResults.suites[j].durationSec;
j++;
}
}
// Decorate the 'jasmine' object with getters
jasmine.getJSReport = function () {
if (jasmine.runnerResults) {
return jasmine.runnerResults;
}
return null;
};
jasmine.getJSReportAsString = function () {
return JSON.stringify(jasmine.getJSReport());
};
}
};
// export public
jasmine.JSReporter = JSReporter;
// ------------------------------------------------------------------------
// Jasmine JSReporter for Jasmine 2.0
// ------------------------------------------------------------------------
/*
Simple timer implementation
*/
var Timer = function () {};
Timer.prototype.start = function () {
this.startTime = new Date().getTime();
return this;
};
Timer.prototype.elapsed = function () {
if (this.startTime == null) {
return -1;
}
return new Date().getTime() - this.startTime;
};
/*
Utility methods
*/
var _extend = function (obj1, obj2) {
for (var prop in obj2) {
obj1[prop] = obj2[prop];
}
return obj1;
};
var _clone = function (obj) {
if (obj !== Object(obj)) {
return obj;
}
return _extend({}, obj);
};
jasmine.JSReporter2 = function () {
this.specs = {};
this.suites = {};
this.rootSuites = [];
this.suiteStack = [];
// export methods under jasmine namespace
jasmine.getJSReport = this.getJSReport;
jasmine.getJSReportAsString = this.getJSReportAsString;
};
var JSR = jasmine.JSReporter2.prototype;
// Reporter API methods
// --------------------
JSR.suiteStarted = function (suite) {
suite = this._cacheSuite(suite);
// build up suite tree as we go
suite.specs = [];
suite.suites = [];
suite.passed = true;
suite.parentId = this.suiteStack.slice(this.suiteStack.length - 1)[0];
if (suite.parentId) {
this.suites[suite.parentId].suites.push(suite);
} else {
this.rootSuites.push(suite.id);
}
this.suiteStack.push(suite.id);
suite.timer = new Timer().start();
};
JSR.suiteDone = function (suite) {
suite = this._cacheSuite(suite);
suite.duration = suite.timer.elapsed();
suite.durationSec = suite.duration / 1000;
this.suiteStack.pop();
// maintain parent suite state
var parent = this.suites[suite.parentId];
if (parent) {
parent.passed = parent.passed && suite.passed;
}
// keep report representation clean
delete suite.timer;
delete suite.id;
delete suite.parentId;
delete suite.fullName;
};
JSR.specStarted = function (spec) {
spec = this._cacheSpec(spec);
spec.timer = new Timer().start();
// build up suites->spec tree as we go
spec.suiteId = this.suiteStack.slice(this.suiteStack.length - 1)[0];
this.suites[spec.suiteId].specs.push(spec);
};
JSR.specDone = function (spec) {
spec = this._cacheSpec(spec);
spec.duration = spec.timer.elapsed();
spec.durationSec = spec.duration / 1000;
spec.skipped = spec.status === 'pending';
spec.passed = spec.skipped || spec.status === 'passed';
spec.totalCount = spec.passedExpectations.length + spec.failedExpectations.length;
spec.passedCount = spec.passedExpectations.length;
spec.failedCount = spec.failedExpectations.length;
spec.failures = [];
for (var i = 0, j = spec.failedExpectations.length; i < j; i++) {
var fail = spec.failedExpectations[i];
spec.failures.push({
type: 'expect',
expected: fail.expected,
passed: false,
message: fail.message,
matcherName: fail.matcherName,
trace: {
stack: fail.stack
}
});
}
// maintain parent suite state
var parent = this.suites[spec.suiteId];
if (spec.failed) {
parent.failingSpecs.push(spec);
}
parent.passed = parent.passed && spec.passed;
// keep report representation clean
delete spec.timer;
delete spec.totalExpectations;
delete spec.passedExpectations;
delete spec.suiteId;
delete spec.fullName;
delete spec.id;
delete spec.status;
delete spec.failedExpectations;
};
JSR.jasmineDone = function () {
this._buildReport();
};
JSR.getJSReport = function () {
if (jasmine.jsReport) {
return jasmine.jsReport;
}
};
JSR.getJSReportAsString = function () {
if (jasmine.jsReport) {
return JSON.stringify(jasmine.jsReport);
}
};
// Private methods
// ---------------
JSR._haveSpec = function (spec) {
return this.specs[spec.id] != null;
};
JSR._cacheSpec = function (spec) {
var existing = this.specs[spec.id];
if (existing == null) {
existing = this.specs[spec.id] = _clone(spec);
} else {
_extend(existing, spec);
}
return existing;
};
JSR._haveSuite = function (suite) {
return this.suites[suite.id] != null;
};
JSR._cacheSuite = function (suite) {
var existing = this.suites[suite.id];
if (existing == null) {
existing = this.suites[suite.id] = _clone(suite);
} else {
_extend(existing, suite);
}
return existing;
};
JSR._buildReport = function () {
var overallDuration = 0;
var overallPassed = true;
var overallSuites = [];
for (var i = 0, j = this.rootSuites.length; i < j; i++) {
var suite = this.suites[this.rootSuites[i]];
overallDuration += suite.duration;
overallPassed = overallPassed && suite.passed;
overallSuites.push(suite);
}
jasmine.jsReport = {
passed: overallPassed,
durationSec: overallDuration / 1000,
suites: overallSuites
};
};
})(jasmine);