forked from michaelleeallen/mocha-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (179 loc) · 5.58 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
'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');
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.toConsole = !!options.toConsole;
options.suiteTitleSeparatedBy = options.suiteTitleSeparatedBy || ' ';
return options;
}
function defaultSuiteTitle(suite) {
return suite.title;
}
function fullSuiteTitle(suite, options) {
var parent = suite.parent;
var title = [ suite.title ];
while (parent) {
title.unshift(parent.title);
parent = parent.parent;
}
title.shift();
return title.join(options.suiteTitleSeparatedBy);
}
function isInvalidSuite(suite) {
return suite.title === '' || suite.tests.length === 0 && suite.suites.length === 0;
}
/**
* 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);
this._runner.on('suite', function(suite) {
if (suite.root) {
suite.title = '';
}
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) {
return {
testsuite: [
{
_attr: {
name: this._generateSuiteTitle(suite, this._options),
timestamp: new Date().toISOString().slice(0,-5),
tests: suite.tests.length
}
}
]
};
};
/**
* 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 config = {
testcase: [{
_attr: {
name: test.fullTitle(),
time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000,
classname: test.fullTitle().split(' ')[0]
}
}]
};
if (err) {
var failureElement = {
_cdata: this.removeInvalidCharacters(err.stack)
};
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);
}
};
/**
* 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;
testsuites.forEach(function(suite) {
var _suiteAttr = suite.testsuite[0]._attr;
var _cases = suite.testsuite.slice(1);
_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;
});
return xml(testsuites, { 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) {
debug('writing file to', filePath);
mkdirp.sync(path.dirname(filePath));
fs.appendFileSync(filePath, xml, 'utf-8');
debug('results written successfully');
}
};