forked from michaelleeallen/mocha-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (99 loc) · 2.92 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
'use-strict';
var xml = require('xml');
var Base = require('mocha').reporters.Base;
var filePath = process.env.MOCHA_FILE || 'test-results.xml';
var fs = require('fs');
module.exports = MochaJUnitReporter;
/**
* JUnit reporter for mocha.js.
* @module mocha-junit-reporter
* @param {EventEmitter} runner - the test runner
*/
function MochaJUnitReporter(runner) {
// 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, this.stats));
}.bind(this));
}
MochaJUnitReporter.prototype.getTestsuiteData = function(suite){
return {
testsuite: [
{
_attr: {
name: suite.title,
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: test.duration,
className: test.title
}
}]
};
if ( err ) {
config.testcase.push({failure: err.message});
}
return config;
};
/**
* Produces an XML string from the given test data.
* @param {array} testcases - a list of xml configs
* @param {number} passes - number of tests passed
* @param {number} failures - number tests failed
* @returns {string}
*/
MochaJUnitReporter.prototype.getXml = function(testsuites, testcases, stats){
var suites = testsuites.map(function(suite, i){
var _suite = Object.create(suite);
var _cases = testcases.slice(i, suite.tests);
_suite.testsuite = _suite.testsuite.concat(_cases);
_suite.testsuite[0]._attr.failures = _cases.reduce(function(num, testcase){
return num + (testcase.testcase.length > 1)? 1 : 0;
}, 0);
_suite.testsuite[0]._attr.timestamp = stats.start;
_suite.testsuite[0]._attr.time = stats.duration;
return _suite;
});
return xml({ testsuites: suites }, { declaration: true });
};
/**
* Writes a JUnit test report XML document.
* @param {string} xml - xml string
*/
MochaJUnitReporter.prototype.writeXmlToDisk = function(xml){
fs.writeFileSync(filePath, xml, 'utf-8');
console.log('test results written to', filePath);
};