-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
reporter.test.ts
235 lines (202 loc) · 7.97 KB
/
reporter.test.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import type { Options, Capabilities } from '@wdio/types'
import BaseReporter from '../src/reporter'
class CustomReporter {
public emit = jest.fn()
public isCustom = true
public inSync = false
constructor (public options: WebdriverIO.ServiceOption) {}
get isSynchronised () {
return this.inSync
}
}
const capability: Capabilities.Capabilities = { browserName: 'foo' }
process.send = jest.fn()
describe('BaseReporter', () => {
it('should load all reporters', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', { foo: 'bar' }]
]
} as Options.Testrunner, '0-0', capability)
expect(reporter['_reporters']).toHaveLength(2)
})
it('getLogFile', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', { foo: 'bar' }]
]
} as Options.Testrunner, '0-0', capability)
expect(reporter.getLogFile('foobar'))
.toMatch(/(\\|\/)foo(\\|\/)bar(\\|\/)wdio-0-0-foobar-reporter.log/)
})
it('can set custom logFile property', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
[CustomReporter, { foo: 'bar', logFile: '/barfoo.log' }],
['dot', { foo: 'bar', logFile: '/foobar.log' }]
],
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
// @ts-expect-error
expect(reporter['_reporters'][0].options.logFile)
.toBe('/barfoo.log')
// @ts-expect-error
expect(reporter['_reporters'][1].options.logFile)
.toBe('/foobar.log')
})
it('should output log file to custom outputDir', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
['dot', {
foo: 'bar',
outputDir: '/foo/bar/baz'
}]
],
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
expect(reporter.getLogFile('dot'))
.toMatch(/(\\|\/)foo(\\|\/)bar(\\|\/)baz(\\|\/)wdio-0-0-dot-reporter.log/)
})
it('should return custom log file name', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', {
foo: 'bar',
outputFileFormat: (options: any) => {
return `wdio-results-${options.cid}.xml`
}
}]
]
} as Options.Testrunner, '0-0', capability)
expect(reporter.getLogFile('dot'))
.toMatch(/(\\|\/)foo(\\|\/)bar(\\|\/)wdio-results-0-0.xml/)
})
it('should throw error if outputFileFormat is not a function', () => {
expect(() => {
new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', {
foo: 'bar',
outputFileFormat: 'foo'
}]
]
} as Options.Testrunner, '0-0', capability)
}).toThrow('outputFileFormat must be a function')
})
test('getLogFile returns undefined if outputDir is not defined', () => {
const reporter = new BaseReporter({
reporters: [
'dot',
['dot', { foo: 'bar' }]
]
} as Options.Testrunner, '0-0', capability)
expect(reporter.getLogFile('foobar')).toBe(undefined)
})
it('should emit events to all reporters', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', { foo: 'bar' }]
]
} as Options.Testrunner, '0-0', capability)
const payload = { foo: [1, 2, 3] }
reporter.emit('runner:start', payload)
expect(reporter['_reporters'].map((r) => (r.emit as jest.Mock).mock.calls)).toEqual([
[['runner:start', Object.assign(payload, { cid: '0-0' })]],
[['runner:start', Object.assign(payload, { cid: '0-0' })]]
])
expect(process.send).not.toBeCalled()
})
it('should send printFailureMessage', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [
'dot',
['dot', { foo: 'bar' }]
]
} as Options.Testrunner, '0-0', capability)
const payload = { foo: [1, 2, 3] }
reporter.emit('test:fail', payload)
expect(reporter['_reporters'].map((r) => (r.emit as jest.Mock).mock.calls)).toEqual([
[['test:fail', Object.assign(payload, { cid: '0-0' })]],
[['test:fail', Object.assign(payload, { cid: '0-0' })]]
])
expect(process.send).toBeCalledTimes(1)
expect((process.send as jest.Mock).mock.calls[0][0].name).toBe('printFailureMessage')
})
it('should allow to load custom reporters', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [CustomReporter] as any,
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
expect(reporter['_reporters']).toHaveLength(1)
// @ts-ignore
expect(reporter['_reporters'][0].isCustom).toBe(true)
})
it('should allow to write to output directory with custom reporter', () => {
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [[CustomReporter, {
outputDir: '/foo/baz/bar'
}]] as any,
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
expect(reporter.getLogFile('CustomReporter')).toMatch(/(\\|\/)foo(\\|\/)baz(\\|\/)bar(\\|\/)wdio-0-0-CustomReporter-reporter.log/)
})
it('should throw if reporters are in a wrong format', () => {
(expect as any as jest.Expect).hasAssertions()
try {
new BaseReporter({
outputDir: '/foo/bar',
reporters: [{ foo: 'bar' } as any],
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
} catch (err: any) {
expect(err.message).toBe('Invalid reporters config')
}
})
it('should have a waitForSync method to allow reporters to sync stuff', async () => {
(expect as any as jest.Expect).hasAssertions()
const start = Date.now()
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [CustomReporter, CustomReporter] as any,
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
// @ts-ignore test reporter param
setTimeout(() => (reporter['_reporters'][0].inSync = true), 100)
// @ts-ignore test reporter param
setTimeout(() => (reporter['_reporters'][1].inSync = true), 200)
await reporter.waitForSync()
expect(Date.now() - start).toBeGreaterThanOrEqual(199)
})
it('it should fail if waitForSync times out', async () => {
(expect as any as jest.Expect).hasAssertions()
const reporter = new BaseReporter({
outputDir: '/foo/bar',
reporters: [CustomReporter] as any,
reporterSyncInterval: 10,
reporterSyncTimeout: 100,
capabilities: [capability]
} as Options.Testrunner, '0-0', capability)
// @ts-ignore test reporter param
setTimeout(() => (reporter['_reporters'][0].inSync = true), 112)
await expect(reporter.waitForSync())
.rejects.toEqual(new Error('Some reporters are still unsynced: CustomReporter'))
})
afterEach(() => {
(process.send as jest.Mock).mockClear()
})
})