forked from Pharb/mocha-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (132 loc) · 4.17 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
'use-strict';
var xml = require('xml');
var Base = require('mocha').reporters.Base;
var fs = require('fs');
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'];
/**
* JUnit reporter for mocha.js.
* @module mocha-junit-reporter
* @param {EventEmitter} runner - the test runner
* @param {Object} options - mocha options
*/
function MochaJUnitReporter(runner, options) {
var filePath;
if (options && options.reporterOptions && options.reporterOptions.mochaFile) {
filePath = options.reporterOptions.mochaFile;
} else {
filePath = process.env.MOCHA_FILE || 'test-results.xml';
}
// a list of all test cases that have run
var testcases = [];
var testsuites = [];
// get functionality from the Base reporter
Base.call(this, runner);
// remove old results
runner.on('start', function() {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
});
runner.on('suite', function(suite){
if (suite.title === '' || suite.tests.length === 0) return;
testsuites.push(this.getTestsuiteData(suite));
}.bind(this));
runner.on('pass', function(test){
testcases.push(this.getTestcaseData(test));
}.bind(this));
runner.on('fail', function(test, err){
testcases.push(this.getTestcaseData(test, err));
}.bind(this));
runner.on('end', function(){
this.writeXmlToDisk(this.getXml(testsuites, testcases, runner.stats), filePath);
}.bind(this));
}
MochaJUnitReporter.prototype.getTestsuiteData = function(suite){
return {
testsuite: [
{
_attr: {
name: suite.title,
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.title
}
}]
};
if ( err ) {
config.testcase.push({failure: this.removeInvalidCharacters(err.message)});
}
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(invalidCharacter, '');
}, input);
};
/**
* Produces an XML string from the given test data.
* @param {Array.<Object>} testsuites - a list of xml configs
* @param {Array.<Object>} testcases - a list of xml configs
* @param {Object} stats - mocha statistics from the runner
* @returns {string}
*/
MochaJUnitReporter.prototype.getXml = function(testsuites, testcases, stats){
var totalSuitesTime = 0,
suites = testsuites.map(function(suite){
var _suite = Object.create(suite),
_suiteAttr = _suite.testsuite[0]._attr,
_cases = testcases.splice(0, _suiteAttr.tests);
_suite.testsuite = _suite.testsuite.concat(_cases);
_suiteAttr.failures = _cases.reduce(function(num, testcase){
return num + ((testcase.testcase.length > 1)? 1 : 0);
}, 0);
_suiteAttr.time = _cases.reduce(function(suitDuration, testcase){
return suitDuration + testcase.testcase[0]._attr.time;
}, 0);
totalSuitesTime += _suiteAttr.time;
return _suite;
});
return xml({
testsuites: [{
_attr: {
name: 'Mocha Tests',
timestamp: stats.start.toISOString().slice(0,-5),
time: totalSuitesTime,
tests: stats.tests,
failures: stats.failures
}
}].concat(suites)
}, { 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){
fs.writeFileSync(filePath, xml, 'utf-8');
console.log('test results written to', filePath);
};