forked from jasongardnerlv/wct-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
120 lines (114 loc) · 3.69 KB
/
plugin.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
var xml = require('xml');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var md5 = require('md5');
module.exports = function(wct, pluginOptions, plugin) {
console.log('starting wct junit plugin');
var outputDir = wct.options.plugins['wct-junit'].outputDir;
var reporters = [];
var lastReporter;
wct.on('test-start', function(browser, test, stats) {
var newReporter = getReporter(reporters, browser, test);
if (lastReporter && newReporter !== lastReporter) {
lastReporter.stats.end = new Date();
lastReporter.stats.duration = new Date() - lastReporter.stats.start;
}
});
wct.on('test-end', function(browser, test, stats) {
var reporter = getReporter(reporters, browser, test);
reporter.stats.tests++;
if (test.state === 'passing') {
reporter.stats.passes++;
} else if (test.state === 'failing') {
reporter.stats.failures++;
} else if (test.state === 'pending') {
reporter.stats.pending++;
}
reporter.tests.push({
suite: test.test[1],
title: test.test[test.test.length - 1],
state: getState(test.state),
pending: (test.state === 'pending'),
duration: test.duration,
err: test.error
});
});
wct.on('browser-end', function(browser, error, stats) {
var testSuites = [];
for (var fileName in reporters[browser.id]) {
if (reporters[browser.id].hasOwnProperty(fileName)) {
var reporter = reporters[browser.id][fileName];
if (!reporter.stats.end) {
reporter.stats.end = new Date();
reporter.stats.duration = new Date() - reporter.stats.start;
}
var reporterStats = reporter.stats;
var testSuite = {testsuite:[{
_attr: {
name: reporter.tests[0].suite,
timestamp: new Date().toISOString().slice(0,-5),
time: reporterStats.duration,
tests: reporterStats.tests,
failures: reporterStats.failures,
skipped: reporterStats.pending
}
}]};
reporter.tests.forEach(function(test) {
var config = {
testcase: [{
_attr: {
name: test.suite + ' - ' + test.title,
time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000,
classname: test.suite
}
}]
};
if (test.err) {
config.testcase.push({failure: {_cdata:test.err.stack}});
}
testSuite.testsuite.push(config);
});
testSuites.push(testSuite);
}
}
var xmlData = xml({testsuites:testSuites},{declaration:true,indent:' '});
var filePath = 'junit.xml';
if (outputDir) {
filePath = path.join(outputDir, filePath);
}
console.log('writing file to', filePath);
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, xmlData, 'utf-8');
});
};
function getState(state) {
if (state === 'passing') {
return 'passed';
} else if (state === 'failing') {
return 'failed';
} else {
return null;
}
}
function getReporter(reporters, browser, test) {
var browserReporters = reporters[browser.id];
if (!browserReporters) {
browserReporters = {};
reporters[browser.id] = browserReporters;
}
var fileName = test.test[0];
var fileReporter = browserReporters[fileName];
if (!fileReporter) {
fileReporter = {};
fileReporter.tests = [];
fileReporter.stats = {};
fileReporter.stats.start = new Date();
fileReporter.stats.tests = 0;
fileReporter.stats.passes = 0;
fileReporter.stats.failures = 0;
fileReporter.stats.pending = 0;
browserReporters[fileName] = fileReporter;
}
return fileReporter;
}