forked from michaelleeallen/mocha-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
502 lines (432 loc) · 14.7 KB
/
index.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
'use strict';
var xml = require('xml');
var Base = require('mocha').reporters.Base;
var fs = require('fs');
var path = require('path');
var debug = require('debug')('mocha-junit-reporter');
var mkdirp = require('mkdirp');
var md5 = require('md5');
var stripAnsi = require('strip-ansi');
// Save timer references so that times are correct even if Date is stubbed.
// See https://github.com/mochajs/mocha/issues/237
var Date = global.Date;
var createStatsCollector;
var mocha6plus;
try {
var json = JSON.parse(
fs.readFileSync(path.dirname(require.resolve('mocha')) + "/package.json", "utf8")
);
var version = json.version;
if (version >= "6") {
createStatsCollector = require("mocha/lib/stats-collector");
mocha6plus = true;
} else {
mocha6plus = false;
}
} catch (e) {
// eslint-disable-next-line no-console
console.warn("Couldn't determine Mocha version");
}
module.exports = MochaJUnitReporter;
// A subset of invalid characters as defined in http://www.w3.org/TR/xml/#charsets that can occur in e.g. stacktraces
// regex lifted from https://github.com/MylesBorins/xml-sanitizer/ (licensed MIT)
var INVALID_CHARACTERS_REGEX = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007f-\u0084\u0086-\u009f\uD800-\uDFFF\uFDD0-\uFDFF\uFFFF\uC008]/g; //eslint-disable-line no-control-regex
function findReporterOptions(options) {
debug('Checking for options in', options);
if (!options) {
debug('No options provided');
return {};
}
if (!mocha6plus) {
debug('Options for pre mocha@6');
return options.reporterOptions || {};
}
if (options.reporterOptions) {
debug('Command-line options for mocha@6+');
return options.reporterOptions;
}
// this is require to handle .mocharc.js files
debug('Looking for .mocharc.js options');
return Object.keys(options).filter(function(key) { return key.indexOf('reporterOptions.') === 0; })
.reduce(function(reporterOptions, key) {
reporterOptions[key.substring('reporterOptions.'.length)] = options[key];
return reporterOptions;
}, {});
}
function configureDefaults(options) {
var config = findReporterOptions(options);
debug('options', config);
config.mochaFile = getSetting(config.mochaFile, 'MOCHA_FILE', 'test-results.xml');
config.attachments = getSetting(config.attachments, 'ATTACHMENTS', false);
config.antMode = getSetting(config.antMode, 'ANT_MODE', false);
config.jenkinsMode = getSetting(config.jenkinsMode, 'JENKINS_MODE', false);
config.properties = getSetting(config.properties, 'PROPERTIES', null, parsePropertiesFromEnv);
config.toConsole = !!config.toConsole;
config.rootSuiteTitle = config.rootSuiteTitle || 'Root Suite';
config.testsuitesTitle = config.testsuitesTitle || 'Mocha Tests';
if (config.antMode) {
updateOptionsForAntMode(config);
}
if (config.jenkinsMode) {
updateOptionsForJenkinsMode(config);
}
config.suiteTitleSeparatedBy = config.suiteTitleSeparatedBy || ' ';
return config;
}
function updateOptionsForAntMode(options) {
options.antHostname = getSetting(options.antHostname, 'ANT_HOSTNAME', process.env.HOSTNAME);
if (!options.properties) {
options.properties = {};
}
}
function updateOptionsForJenkinsMode(options) {
if (options.useFullSuiteTitle === undefined) {
options.useFullSuiteTitle = true;
}
debug('jenkins mode - testCaseSwitchClassnameAndName', options.testCaseSwitchClassnameAndName);
if (options.testCaseSwitchClassnameAndName === undefined) {
options.testCaseSwitchClassnameAndName = true;
}
if (options.suiteTitleSeparatedBy === undefined) {
options.suiteTitleSeparatedBy = '.';
}
}
/**
* Determine an option value.
* 1. If `key` is present in the environment, then use the environment value
* 2. If `value` is specified, then use that value
* 3. Fall back to `defaultVal`
* @module mocha-junit-reporter
* @param {Object} value - the value from the reporter options
* @param {String} key - the environment variable to check
* @param {Object} defaultVal - the fallback value
* @param {function} transform - a transformation function to be used when loading values from the environment
*/
function getSetting(value, key, defaultVal, transform) {
if (process.env[key] !== undefined) {
var envVal = process.env[key];
return (typeof transform === 'function') ? transform(envVal) : envVal;
}
if (value !== undefined) {
return value;
}
return defaultVal;
}
function defaultSuiteTitle(suite) {
if (suite.root && suite.title === '') {
return stripAnsi(this._options.rootSuiteTitle);
}
return stripAnsi(suite.title);
}
function fullSuiteTitle(suite) {
var parent = suite.parent;
var title = [ suite.title ];
while (parent) {
if (parent.root && parent.title === '') {
title.unshift(this._options.rootSuiteTitle);
} else {
title.unshift(parent.title);
}
parent = parent.parent;
}
return stripAnsi(title.join(this._options.suiteTitleSeparatedBy));
}
function isInvalidSuite(suite) {
return (!suite.root && suite.title === '') || (suite.tests.length === 0 && suite.suites.length === 0);
}
function parsePropertiesFromEnv(envValue) {
if (envValue) {
debug('Parsing from env', envValue);
return envValue.split(',').reduce(function(properties, prop) {
var property = prop.split(':');
properties[property[0]] = property[1];
return properties;
}, []);
}
return null;
}
function generateProperties(options) {
var props = options.properties;
if (!props) {
return [];
}
return Object.keys(props).reduce(function(properties, name) {
var value = props[name];
properties.push({ property: { _attr: { name: name, value: value } } });
return properties;
}, []);
}
function getJenkinsClassname (test, options) {
debug('Building jenkins classname for', test);
var parent = test.parent;
var titles = [];
while (parent) {
parent.title && titles.unshift(parent.title);
parent = parent.parent;
}
return titles.join(options.suiteTitleSeparatedBy);
}
/**
* JUnit reporter for mocha.js.
* @module mocha-junit-reporter
* @param {EventEmitter} runner - the test runner
* @param {Object} options - mocha options
*/
function MochaJUnitReporter(runner, options) {
if (mocha6plus) {
createStatsCollector(runner);
}
this._options = configureDefaults(options);
this._runner = runner;
this._generateSuiteTitle = this._options.useFullSuiteTitle ? fullSuiteTitle : defaultSuiteTitle;
this._antId = 0;
this._Date = (options || {}).Date || Date;
var testsuites = [];
this._testsuites = testsuites;
function lastSuite() {
return testsuites[testsuites.length - 1].testsuite;
}
// get functionality from the Base reporter
Base.call(this, runner);
// remove old results
this._runner.on('start', function() {
if (fs.existsSync(this._options.mochaFile)) {
debug('removing report file', this._options.mochaFile);
fs.unlinkSync(this._options.mochaFile);
}
}.bind(this));
this._onSuiteBegin = function(suite) {
if (!isInvalidSuite(suite)) {
testsuites.push(this.getTestsuiteData(suite));
}
};
this._runner.on('suite', function(suite) {
// allow tests to mock _onSuiteBegin
return this._onSuiteBegin(suite);
}.bind(this));
this._onSuiteEnd = function(suite) {
if (!isInvalidSuite(suite)) {
var testsuite = lastSuite();
if (testsuite) {
var start = testsuite[0]._attr.timestamp;
testsuite[0]._attr.time = this._Date.now() - start;
}
}
};
this._runner.on('suite end', function(suite) {
// allow tests to mock _onSuiteEnd
return this._onSuiteEnd(suite);
}.bind(this));
this._runner.on('pass', function(test) {
lastSuite().push(this.getTestcaseData(test));
}.bind(this));
this._runner.on('fail', function(test, err) {
lastSuite().push(this.getTestcaseData(test, err));
}.bind(this));
if (this._options.includePending) {
this._runner.on('pending', function(test) {
var testcase = this.getTestcaseData(test);
testcase.testcase.push({ skipped: null });
lastSuite().push(testcase);
}.bind(this));
}
this._runner.on('end', function(){
this.flush(testsuites);
}.bind(this));
}
/**
* Produces an xml node for a test suite
* @param {Object} suite - a test suite
* @return {Object} - an object representing the xml node
*/
MochaJUnitReporter.prototype.getTestsuiteData = function(suite) {
var antMode = this._options.antMode;
var _attr = {
name: this._generateSuiteTitle(suite),
timestamp: this._Date.now(),
tests: suite.tests.length
};
var testSuite = { testsuite: [ { _attr: _attr } ] };
if(suite.file) {
testSuite.testsuite[0]._attr.file = suite.file;
}
var properties = generateProperties(this._options);
if (properties.length || antMode) {
testSuite.testsuite.push({
properties: properties
});
}
if (antMode) {
_attr.package = _attr.name;
_attr.hostname = this._options.antHostname;
_attr.id = this._antId;
_attr.errors = 0;
this._antId += 1;
}
return testSuite;
};
/**
* Produces an xml config for a given test case.
* @param {object} test - test case
* @param {object} err - if test failed, the failure object
* @returns {object}
*/
MochaJUnitReporter.prototype.getTestcaseData = function(test, err) {
var jenkinsMode = this._options.jenkinsMode;
var flipClassAndName = this._options.testCaseSwitchClassnameAndName;
var name = stripAnsi(jenkinsMode ? getJenkinsClassname(test, this._options) : test.fullTitle());
var classname = stripAnsi(test.title);
var testcase = {
testcase: [{
_attr: {
name: flipClassAndName ? classname : name,
time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000,
classname: flipClassAndName ? name : classname
}
}]
};
// We need to merge console.logs and attachments into one <system-out> -
// see JUnit schema (only accepts 1 <system-out> per test).
var systemOutLines = [];
if (this._options.outputs && (test.consoleOutputs && test.consoleOutputs.length > 0)) {
systemOutLines = systemOutLines.concat(test.consoleOutputs);
}
if (this._options.attachments && test.attachments && test.attachments.length > 0) {
systemOutLines = systemOutLines.concat(test.attachments.map(
function (file) {
return '[[ATTACHMENT|' + file + ']]';
}
));
}
if (systemOutLines.length > 0) {
testcase.testcase.push({'system-out': this.removeInvalidCharacters(stripAnsi(systemOutLines.join('\n')))});
}
if (this._options.outputs && (test.consoleErrors && test.consoleErrors.length > 0)) {
testcase.testcase.push({'system-err': this.removeInvalidCharacters(stripAnsi(test.consoleErrors.join('\n')))});
}
if (err) {
var message;
if (err.message && typeof err.message.toString === 'function') {
message = err.message + '';
} else if (typeof err.inspect === 'function') {
message = err.inspect() + '';
} else {
message = '';
}
var failureMessage = err.stack || message;
var failureElement = {
_attr: {
message: this.removeInvalidCharacters(message) || '',
type: err.name || ''
},
_cdata: this.removeInvalidCharacters(failureMessage)
};
testcase.testcase.push({failure: failureElement});
}
return testcase;
};
/**
* @param {string} input
* @returns {string} without invalid characters
*/
MochaJUnitReporter.prototype.removeInvalidCharacters = function(input){
if (!input) {
return input;
}
return input.replace(INVALID_CHARACTERS_REGEX, '');
};
/**
* Writes xml to disk and ouputs content if "toConsole" is set to true.
* @param {Array.<Object>} testsuites - a list of xml configs
*/
MochaJUnitReporter.prototype.flush = function(testsuites){
this._xml = this.getXml(testsuites);
this.writeXmlToDisk(this._xml, this._options.mochaFile);
if (this._options.toConsole === true) {
console.log(this._xml); // eslint-disable-line no-console
}
};
/**
* Produces an XML string from the given test data.
* @param {Array.<Object>} testsuites - a list of xml configs
* @returns {string}
*/
MochaJUnitReporter.prototype.getXml = function(testsuites) {
var totalTests = 0;
var stats = this._runner.stats;
var antMode = this._options.antMode;
var hasProperties = (!!this._options.properties) || antMode;
var Date = this._Date;
testsuites.forEach(function(suite) {
var _suiteAttr = suite.testsuite[0]._attr;
// testsuite is an array: [attrs, properties?, testcase, testcase, …]
// we want to make sure that we are grabbing test cases at the correct index
var _casesIndex = hasProperties ? 2 : 1;
var _cases = suite.testsuite.slice(_casesIndex);
var missingProps;
// suiteTime has unrounded time as a Number of milliseconds
var suiteTime = _suiteAttr.time;
_suiteAttr.time = (suiteTime / 1000 || 0).toFixed(4);
_suiteAttr.timestamp = new Date(_suiteAttr.timestamp).toISOString().slice(0, -5);
_suiteAttr.failures = 0;
_suiteAttr.skipped = 0;
_cases.forEach(function(testcase) {
var lastNode = testcase.testcase[testcase.testcase.length - 1];
_suiteAttr.skipped += Number('skipped' in lastNode);
_suiteAttr.failures += Number('failure' in lastNode);
testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(4);
});
if (antMode) {
missingProps = ['system-out', 'system-err'];
suite.testsuite.forEach(function(item) {
missingProps = missingProps.filter(function(prop) {
return !item[prop];
});
});
missingProps.forEach(function(prop) {
var obj = {};
obj[prop] = [];
suite.testsuite.push(obj);
});
}
if (!_suiteAttr.skipped) {
delete _suiteAttr.skipped;
}
totalTests += _suiteAttr.tests;
});
if (!antMode) {
var rootSuite = {
_attr: {
name: this._options.testsuitesTitle,
time: (stats.duration / 1000 || 0).toFixed(4),
tests: totalTests,
failures: stats.failures
}
};
if (stats.pending) {
rootSuite._attr.skipped = stats.pending;
}
testsuites = [ rootSuite ].concat(testsuites);
}
return xml({ testsuites: testsuites }, { declaration: true, indent: ' ' });
};
/**
* Writes a JUnit test report XML document.
* @param {string} xml - xml string
* @param {string} filePath - path to output file
*/
MochaJUnitReporter.prototype.writeXmlToDisk = function(xml, filePath){
if (filePath) {
if (filePath.indexOf('[hash]') !== -1) {
filePath = filePath.replace('[hash]', md5(xml));
}
debug('writing file to', filePath);
mkdirp.sync(path.dirname(filePath));
try {
fs.writeFileSync(filePath, xml, 'utf-8');
} catch (exc) {
debug('problem writing results: ' + exc);
}
debug('results written successfully');
}
};