-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
reporter.ts
195 lines (170 loc) · 6.21 KB
/
reporter.ts
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
import path from 'path'
import logger from '@wdio/logger'
import { initialisePlugin } from '@wdio/utils'
import type { Options, Capabilities, Reporters } from '@wdio/types'
import { sendFailureMessage } from './utils'
const log = logger('@wdio/runner')
/**
* BaseReporter
* responsible for initialising reporters for every testrun and propagating events
* to all these reporters
*/
export default class BaseReporter {
private _reporters: Reporters.ReporterInstance[]
constructor(
private _config: Options.Testrunner,
private _cid: string,
public caps: Capabilities.RemoteCapability
) {
// ensure all properties are set before initializing the reporters
this._reporters = this._config.reporters!.map(this.initReporter.bind(this))
}
/**
* emit events to all registered reporter and wdio launcer
*
* @param {String} e event name
* @param {object} payload event payload
*/
emit (e: string, payload: any) {
payload.cid = this._cid
/**
* Send failure message (only once) in case of test or hook failure
*/
sendFailureMessage(e, payload)
this._reporters.forEach((reporter) => reporter.emit(e, payload))
}
getLogFile (name: string) {
// clone the config to avoid changing original properties
let options = Object.assign({}, this._config) as any
let filename = `wdio-${this._cid}-${name}-reporter.log`
const reporterOptions = this._config.reporters!.find((reporter) => (
Array.isArray(reporter) &&
(
reporter[0] === name ||
typeof reporter[0] === 'function' && reporter[0].name === name
)
)) as { outputFileFormat?: Function }[]
if (reporterOptions) {
const fileformat = reporterOptions[1].outputFileFormat
options.cid = this._cid
options.capabilities = this.caps
Object.assign(options, reporterOptions[1])
if (fileformat) {
if (typeof fileformat !== 'function') {
throw new Error('outputFileFormat must be a function')
}
filename = fileformat(options)
}
}
if (!options.outputDir) {
return
}
return path.join(options.outputDir, filename)
}
/**
* return write stream object based on reporter name
*/
getWriteStreamObject (reporter: string) {
return {
write: /* istanbul ignore next */ (content: unknown) => process.send!({
origin: 'reporter',
name: reporter,
content
})
}
}
/**
* wait for reporter to finish synchronization, e.g. when sending data asynchronous
* to a server (e.g. sumo reporter)
*/
waitForSync() {
const startTime = Date.now()
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const unsyncedReporter = this._reporters
.filter((reporter) => !reporter.isSynchronised)
.map((reporter) => reporter.constructor.name)
if ((Date.now() - startTime) > this._config.reporterSyncTimeout! && unsyncedReporter.length) {
clearInterval(interval)
return reject(new Error(`Some reporters are still unsynced: ${unsyncedReporter.join(', ')}`))
}
/**
* no reporter are in need to sync anymore, continue
*/
if (!unsyncedReporter.length) {
clearInterval(interval)
return resolve(true)
}
log.info(`Wait for ${unsyncedReporter.length} reporter to synchronise`)
// wait otherwise
}, this._config.reporterSyncInterval)
})
}
/**
* initialise reporters
*/
initReporter (reporter: Reporters.ReporterEntry) {
let ReporterClass: Reporters.ReporterClass
let options: Partial<Reporters.Options> = {}
/**
* check if reporter has custom options
*/
if (Array.isArray(reporter)) {
options = Object.assign({}, options, reporter[1])
reporter = reporter[0]
}
/**
* check if reporter was passed in from a file, e.g.
*
* ```js
* const MyCustomReporter = require('/some/path/MyCustomReporter.js')
* exports.config
* //...
* reporters: [
* MyCustomReporter, // or
* [MyCustomReporter, { custom: 'option' }]
* ]
* //...
* }
* ```
*/
if (typeof reporter === 'function') {
ReporterClass = reporter as Reporters.ReporterClass
options.logFile = options.setLogFile
? options.setLogFile(this._cid, ReporterClass.name)
: typeof options.logFile === 'string'
? options.logFile
: this.getLogFile(ReporterClass.name)
options.writeStream = this.getWriteStreamObject(ReporterClass.name)
return new ReporterClass(options)
}
/**
* check if reporter is a node package, e.g. wdio-dot reporter
*
* ```js
* exports.config
* //...
* reporters: [
* 'dot', // or
* ['dot', { custom: 'option' }]
* ]
* //...
* }
* ```
*/
if (typeof reporter === 'string') {
ReporterClass = initialisePlugin(reporter, 'reporter').default as Reporters.ReporterClass
options.logFile = options.setLogFile
? options.setLogFile(this._cid, reporter)
: typeof options.logFile === 'string'
? options.logFile
: this.getLogFile(reporter)
options.writeStream = this.getWriteStreamObject(reporter)
return new ReporterClass(options)
}
/**
* throw error if reporter property was invalid
*/
throw new Error('Invalid reporters config')
}
}