This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
/
reporter.js
197 lines (177 loc) · 7.34 KB
/
reporter.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
import events from 'events'
import junit from 'junit-report-builder'
import path from 'path'
import fs from 'fs'
import mkdirp from 'mkdirp'
import get from 'lodash.get'
/**
* Initialize a new `Junit` test reporter.
*
* @param {Runner} runner
* @api public
*/
class JunitReporter extends events.EventEmitter {
constructor (baseReporter, config, options = {}) {
super()
this.baseReporter = baseReporter
this.config = config
this.options = options
this.suiteNameRegEx = this.options.suiteNameFormat instanceof RegExp
? this.options.suiteNameFormat
: /[^a-zA-Z0-9]+/
this.on('end', ::this.onEnd)
}
onEnd () {
const outputIsFunction = typeof this.options.outputFileFormat === 'function'
if (outputIsFunction) {
// backward compatibility
this.options.outputFileFormat = {
multi: this.options.outputFileFormat
}
}
const noFileOutputDefined = !this.options.outputFileFormat
const hasSingleFileOutput = get(this.options, 'outputFileFormat.single')
const hasMultiFileOutput = get(this.options, 'outputFileFormat.multi')
if (noFileOutputDefined || hasMultiFileOutput) {
this.onMultiFileOutput()
}
if (hasSingleFileOutput) {
this.onSingleFileOutput()
}
const { epilogue } = this.baseReporter
epilogue.call(this.baseReporter)
}
onSingleFileOutput () {
const xml = this.prepareXml(this.baseReporter.stats.runners)
let filename = `WDIO.xunit.all.xml` // default
if (typeof get(this.options, 'outputFileFormat.single') === 'function') {
filename = this.options.outputFileFormat.single({
config: this.config
})
}
this.write(filename, xml)
}
onMultiFileOutput () {
for (let cid of Object.keys(this.baseReporter.stats.runners)) {
const capabilities = this.baseReporter.stats.runners[cid]
const singleRunner = {}
singleRunner[cid] = capabilities
const xml = this.prepareXml(singleRunner)
let filename = `WDIO.xunit.${capabilities.sanitizedCapabilities}.${cid}.xml` // default
if (typeof get(this.options, 'outputFileFormat.multi') === 'function') {
filename = this.options.outputFileFormat.multi({
capabilities: capabilities.sanitizedCapabilities,
cid: cid,
config: this.config
})
}
this.write(filename, xml)
}
}
prepareName (name = 'Skipped test') {
return name.split(this.suiteNameRegEx).filter(
(item) => item && item.length
).join('_')
}
prepareXml (runners) {
const builder = junit.newBuilder()
for (const key of Object.keys(runners)) {
const capabilities = runners[key]
const packageName = this.options.packageName
? `${capabilities.sanitizedCapabilities}-${this.options.packageName}`
: capabilities.sanitizedCapabilities
for (let specId of Object.keys(capabilities.specs)) {
const spec = capabilities.specs[specId]
for (let suiteKey of Object.keys(spec.suites)) {
/**
* ignore root before all
*/
/* istanbul ignore if */
if (suiteKey.match(/^"before all"/)) {
continue
}
const suite = spec.suites[suiteKey]
const suiteName = this.prepareName(suite.title)
const testSuite = builder.testSuite()
.name(suiteName)
.timestamp(suite.start)
.time(suite.duration / 1000)
.property('specId', specId)
.property('suiteName', suite.title)
.property('capabilities', capabilities.sanitizedCapabilities)
.property('file', spec.files[0].replace(process.cwd(), '.'))
for (let testKey of Object.keys(suite.tests)) {
if (testKey !== 'undefined') { // fix cucumber hooks crashing reporter
const test = suite.tests[testKey]
const testName = this.prepareName(test.title)
const testCase = testSuite.testCase()
.className(`${packageName}.${suiteName}`)
.name(testName)
.time(test.duration / 1000)
if (test.state === 'pending') {
testCase.skipped()
}
if (test.error) {
const errorOptions = this.options.errorOptions
if (errorOptions) {
for (const key of Object.keys(errorOptions)) {
testCase[key](test.error[errorOptions[key]])
}
} else {
// default
testCase.error(test.error.message)
}
testCase.standardError(`\n${test.error.stack}\n`)
}
const output = this.getStandardOutput(test)
if (output) testCase.standardOutput(`\n${output}\n`)
}
}
}
}
}
return builder.build()
}
getStandardOutput (test) {
/* istanbul ignore if */
if (this.options.writeStandardOutput === false) {
return ''
}
let standardOutput = []
test.output.forEach((data) => {
switch (data.type) {
case 'command':
standardOutput.push(
`COMMAND: ${data.payload.method.toUpperCase()} ` +
`${data.payload.uri.href} - ${this.format(data.payload.data)}`
)
break
case 'result':
standardOutput.push(`RESULT: ${this.format(data.payload.body)}`)
break
}
})
return standardOutput.length ? standardOutput.join('\n') : ''
}
write (filename, xml) {
/* istanbul ignore if */
if (!this.options || typeof this.options.outputDir !== 'string') {
return console.log(`Cannot write xunit report: empty or invalid 'outputDir'.`)
}
try {
const dir = path.resolve(this.options.outputDir)
const filepath = path.join(dir, filename)
mkdirp.sync(dir)
fs.writeFileSync(filepath, xml)
console.log(`Wrote xunit report "${filename}" to [${this.options.outputDir}].`)
} catch (e) {
/* istanbul ignore next */
console.log(`Failed to write xunit report "${filename}"
to [${this.options.outputDir}]. Error: ${e}`)
}
}
format (val) {
return JSON.stringify(this.baseReporter.limit(val))
}
}
export default JunitReporter