forked from michaelleeallen/mocha-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
325 lines (275 loc) · 8.79 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
'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');
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
var INVALID_CHARACTERS = ['\u001b'];
function configureDefaults(options) {
debug(options);
options = options || {};
options = options.reporterOptions || {};
options.mochaFile = options.mochaFile || process.env.MOCHA_FILE || 'test-results.xml';
options.properties = options.properties || parsePropertiesFromEnv(process.env.PROPERTIES) || null;
options.toConsole = !!options.toConsole;
options.testCaseSwitchClassnameAndName = options.testCaseSwitchClassnameAndName || false;
options.suiteTitleSeparedBy = options.suiteTitleSeparedBy || ' ';
options.suiteTitleSeparatedBy = options.suiteTitleSeparatedBy || options.suiteTitleSeparedBy || ' ';
options.rootSuiteTitle = options.rootSuiteTitle || 'Root Suite';
options.testsuitesTitle = options.testsuitesTitle || 'Mocha Tests';
return options;
}
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) {
var properties = null;
if (envValue) {
properties = {};
var propertiesArray = envValue.split(',');
for (var i = 0; i < propertiesArray.length; i++) {
var propertyArgs = propertiesArray[i].split(':');
properties[propertyArgs[0]] = propertyArgs[1];
}
}
return properties;
}
function generateProperties(options) {
var properties = [];
for (var propertyName in options.properties) {
if (options.properties.hasOwnProperty(propertyName)) {
properties.push({
property: {
_attr: {
name: propertyName,
value: options.properties[propertyName]
}
}
});
}
}
return properties;
}
/**
* JUnit reporter for mocha.js.
* @module mocha-junit-reporter
* @param {EventEmitter} runner - the test runner
* @param {Object} options - mocha options
*/
function MochaJUnitReporter(runner, options) {
this._options = configureDefaults(options);
this._runner = runner;
this._generateSuiteTitle = this._options.useFullSuiteTitle ? fullSuiteTitle : defaultSuiteTitle;
var 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._runner.on('suite', function(suite) {
if (!isInvalidSuite(suite)) {
testsuites.push(this.getTestsuiteData(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 testSuite = {
testsuite: [
{
_attr: {
name: this._generateSuiteTitle(suite),
timestamp: new Date().toISOString().slice(0,-5),
tests: suite.tests.length
}
}
]
};
if(suite.file) {
testSuite.testsuite[0]._attr.file = suite.file;
}
var properties = generateProperties(this._options);
if (properties.length) {
testSuite.testsuite.push({
properties: properties
});
}
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 flipClassAndName = this._options.testCaseSwitchClassnameAndName;
var name = stripAnsi(test.fullTitle());
var classname = stripAnsi(test.title)
var config = {
testcase: [{
_attr: {
name: flipClassAndName ? classname : name,
time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000,
classname: flipClassAndName ? name : classname
}
}]
};
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: err.message || '',
type: err.name || ''
},
_cdata: this.removeInvalidCharacters(failureMessage)
};
config.testcase.push({failure: failureElement});
}
return config;
};
/**
* @param {string} input
* @returns {string} without invalid characters
*/
MochaJUnitReporter.prototype.removeInvalidCharacters = function(input){
return INVALID_CHARACTERS.reduce(function (text, invalidCharacter) {
return text.replace(new RegExp(invalidCharacter, 'g'), '');
}, input);
};
/**
* 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){
var xml = this.getXml(testsuites);
this.writeXmlToDisk(xml, this._options.mochaFile);
if (this._options.toConsole === true) {
console.log(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 totalSuitesTime = 0;
var totalTests = 0;
var stats = this._runner.stats;
var hasProperties = !!this._options.properties;
testsuites.forEach(function(suite) {
var _suiteAttr = suite.testsuite[0]._attr;
// properties are added before test cases so 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);
_suiteAttr.failures = 0;
_suiteAttr.time = 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);
_suiteAttr.time += testcase.testcase[0]._attr.time;
});
if (!_suiteAttr.skipped) {
delete _suiteAttr.skipped;
}
totalSuitesTime += _suiteAttr.time;
totalTests += _suiteAttr.tests;
});
var rootSuite = {
_attr: {
name: this._options.testsuitesTitle,
time: totalSuitesTime,
tests: totalTests,
failures: stats.failures
}
};
if (stats.pending) {
rootSuite._attr.skipped = stats.pending;
}
return xml({
testsuites: [ rootSuite ].concat(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');
}
};