forked from JamesMGreene/qunit-reporter-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqunit-reporter-junit.js
330 lines (272 loc) · 7.65 KB
/
qunit-reporter-junit.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
/**
* JUnit reporter for QUnit v1.0.2pre
*
* https://github.com/jquery/qunit-reporter-junit
*
* Copyright 2013 jQuery Foundation and other contributors
* Released under the MIT license.
* https://jquery.org/license/
*/
(function() {
'use strict';
var currentRun, currentModule, currentTest = {}, assertCount;
// Gets called when a report is generated.
QUnit.jUnitReport = function(/* data */) {
// Override me!
};
QUnit.begin(function() {
currentRun = {
modules: [],
total: 0,
passed: 0,
failed: 0,
start: new Date(),
time: 0
};
});
function getUsefulModuleName(dataName) {
try {
if(dataName === undefined) {
var i = location.pathname.lastIndexOf('/');
var name = location.pathname.substring(i + 1, location.pathname.length);
i = name.indexOf('.html', name.length - '.html'.length);
if(i !== -1) {
name = name.substring(0, i);
}
return name;
}
} catch(e) {}
return dataName;
}
QUnit.moduleStart(function(data) {
currentModule = {
name: getUsefulModuleName(data.name),
tests: [],
total: 0,
passed: 0,
failed: 0,
start: new Date(),
time: 0,
stdout: [],
stderr: []
};
currentRun.modules.push(currentModule);
});
QUnit.testStart(function(data) {
if (!currentModule) {
currentModule = {
name: getUsefulModuleName() || 'default',
tests: [],
total: 0,
passed: 0,
failed: 0,
start: new Date(),
time: 0,
stdout: [],
stderr: []
};
currentRun.modules.push(currentModule);
}
// Reset the assertion count
assertCount = 0;
currentTest[data.name] = {
name: data.name,
failedAssertions: [],
total: 0,
passed: 0,
failed: 0,
start: new Date(),
time: 0
};
currentModule.tests.push(currentTest[data.name]);
});
QUnit.log(function(data) {
assertCount++;
// Ignore passing assertions
if (!data.result) {
currentTest[data.name].failedAssertions.push(data);
// Add log message of failure to make it easier to find in Jenkins CI
currentModule.stdout.push('[' + currentModule.name + ', ' + data.name + ', ' + assertCount + '] ' + data.message);
}
});
QUnit.testDone(function(data) {
var t = currentTest[data.name];
delete currentTest[data.name];
t.time = (new Date()).getTime() - t.start.getTime(); // ms
t.total = data.total;
t.passed = data.passed;
t.failed = data.failed;
});
QUnit.moduleDone(function(data) {
currentModule.time = (new Date()).getTime() - currentModule.start.getTime(); // ms
currentModule.total = data.total;
currentModule.passed = data.passed;
currentModule.failed = data.failed;
currentModule = null;
});
QUnit.done(function(data) {
currentRun.time = data.runtime || ((new Date()).getTime() - currentRun.start.getTime()); // ms
currentRun.total = data.total;
currentRun.passed = data.passed;
currentRun.failed = data.failed;
generateReport(data, currentRun);
});
var generateReport = function(results, run) {
var pad = function(n) {
return n < 10 ? '0' + n : n;
};
var toISODateString = function(d) {
return d.getUTCFullYear() + '-' +
pad(d.getUTCMonth() + 1)+'-' +
pad(d.getUTCDate()) + 'T' +
pad(d.getUTCHours()) + ':' +
pad(d.getUTCMinutes()) + ':' +
pad(d.getUTCSeconds()) + 'Z';
};
var convertMillisToSeconds = function(ms) {
return Math.round(ms * 1000) / 1000000;
};
var xmlEncode = function(text) {
var baseEntities = {
'"' : '"',
'\'': ''',
'<' : '<',
'>' : '>',
'&' : '&'
};
return ('' + text).replace(/[<>&\"\']/g, function(chr) {
return baseEntities[chr] || chr;
});
};
var XmlWriter = function(settings) {
settings = settings || {};
var data = [], stack = [], lineBreakAt;
var addLineBreak = function(name) {
if (lineBreakAt[name] && data[data.length - 1] !== '\n') {
data.push('\n');
}
};
lineBreakAt = (function(items) {
var i, map = {};
items = items || [];
i = items.length;
while (i--) {
map[items[i]] = {};
}
return map;
})(settings.linebreak_at);
this.start = function(name, attrs, empty) {
if (!empty) {
stack.push(name);
}
data.push('<' + name);
for (var aname in attrs) {
data.push(' ' + xmlEncode(aname) + '="' + xmlEncode(attrs[aname]) + '"');
}
data.push(empty ? ' />' : '>');
addLineBreak(name);
};
this.end = function() {
var name = stack.pop();
addLineBreak(name);
data.push('</' + name + '>');
addLineBreak(name);
};
this.text = function(text) {
data.push(xmlEncode(text));
};
this.cdata = function(text) {
data.push('<![CDATA[' + text + ']]>');
};
this.comment = function(text) {
data.push('<!--' + text + '-->');
};
this.pi = function(name, text) {
data.push('<?' + name + (text ? ' ' + text : '') + '?>\n');
};
this.doctype = function(text) {
data.push('<!DOCTYPE' + text + '>\n');
};
this.getString = function() {
while (stack.length) {
this.end(); // internally calls `stack.pop();`
}
return data.join('').replace(/\n$/, '');
};
this.reset = function() {
data.length = 0;
stack.length = 0;
};
// Start by writing the XML declaration
this.pi(settings.xmldecl || 'xml version="1.0" encoding="UTF-8"');
};
// Generate JUnit XML report!
var m, mLen, module, t, tLen, test, a, aLen, assertion, isEmptyElement,
xmlWriter = new XmlWriter({
linebreak_at: ['testsuites', 'testsuite', 'testcase', 'failure', 'system-out', 'system-err']
});
xmlWriter.start('testsuites', {
name: (window && window.location && window.location.href) || (run.modules.length === 1 && run.modules[0].name) || null,
hostname: 'localhost',
tests: run.total,
failures: run.failed,
errors: 0,
time: convertMillisToSeconds(run.time), // ms → sec
timestamp: toISODateString(run.start)
});
for (m = 0, mLen = run.modules.length; m < mLen; m++) {
module = run.modules[m];
xmlWriter.start('testsuite', {
id: m,
name: module.name,
hostname: 'localhost',
tests: module.total,
failures: module.failed,
errors: 0,
time: convertMillisToSeconds(module.time), // ms → sec
timestamp: toISODateString(module.start)
});
for (t = 0, tLen = module.tests.length; t < tLen; t++) {
test = module.tests[t];
xmlWriter.start('testcase', {
name: test.name,
tests: test.total,
failures: test.failed,
errors: 0,
time: convertMillisToSeconds(test.time), // ms → sec
timestamp: toISODateString(test.start)
});
for (a = 0, aLen = test.failedAssertions.length; a < aLen; a++) {
assertion = test.failedAssertions[a];
isEmptyElement = assertion && !(assertion.actual && assertion.expected);
xmlWriter.start('failure', { type: 'AssertionFailedError', message: assertion.message }, isEmptyElement);
if (!isEmptyElement) {
xmlWriter.start('actual', { value: assertion.actual }, true);
xmlWriter.start('expected', { value: assertion.expected }, true);
xmlWriter.end(); //'failure'
}
}
xmlWriter.end(); //'testcase'
}
// Per-module stdout
if (module.stdout && module.stdout.length) {
xmlWriter.start('system-out');
xmlWriter.cdata('\n' + module.stdout.join('\n') + '\n');
xmlWriter.end(); //'system-out'
}
// Per-module stderr
if (module.stderr && module.stderr.length) {
xmlWriter.start('system-err');
xmlWriter.cdata('\n' + module.stderr.join('\n') + '\n');
xmlWriter.end(); //'system-err'
}
xmlWriter.end(); //'testsuite'
}
xmlWriter.end(); //'testsuites'
// Invoke the user-defined callback
QUnit.jUnitReport({
results: results,
xml: xmlWriter.getString()
});
};
})();