-
Notifications
You must be signed in to change notification settings - Fork 659
/
index.ts
612 lines (498 loc) · 20.2 KB
/
index.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import {
find,
sortBy,
union,
} from 'lodash';
import { writable as isWritableStream } from 'is-stream';
import ReporterPluginHost from './plugin-host';
import ReporterPluginMethod from './plugin-methods';
import formatCommand from './command/format-command';
import { ReporterPluginError } from '../errors/runtime';
import Task from '../runner/task';
import { Writable as WritableStream, Writable } from 'stream';
import { WriteStream } from 'tty';
import TestRun from '../test-run';
import Test from '../api/structure/test';
import Fixture from '../api/structure/fixture';
import TestRunErrorFormattableAdapter from '../errors/test-run/formattable-adapter';
import { CommandBase } from '../test-run/commands/base';
import {
ReporterPlugin,
ReporterPluginSource,
ReporterSource,
} from './interfaces';
import { getPluginFactory, processReporterName } from '../utils/reporter';
import resolvePathRelativelyCwd from '../utils/resolve-path-relatively-cwd';
import makeDir from 'make-dir';
import path from 'path';
import fs from 'fs';
import MessageBus from '../utils/message-bus';
interface PendingPromise {
resolve: Function | null;
then: Function;
}
interface TaskInfo {
task: Task | null;
passed: number;
failed: number;
skipped: number;
testCount: number;
testQueue: TestInfo[];
readonly stopOnFirstFail: boolean;
readonly pendingTaskDonePromise: PendingPromise;
}
interface TestInfo {
fixture: Fixture;
test: Test;
testRunIds: string[];
screenshotPath: null | string;
screenshots: unknown[];
videos: unknown[];
quarantine: null | Record<string, object>;
errs: TestRunErrorFormattableAdapter[];
warnings: string[];
unstable: boolean;
startTime: null | number;
testRunInfo: null | TestRunInfo;
pendingRuns: number;
pendingStarts: number;
pendingTestRunDonePromise: PendingPromise;
pendingTestRunStartPromise: PendingPromise;
browsers: BrowserInfo[];
}
interface BrowserInfo extends Browser {
testRunId: string;
quarantineAttemptsTestRunIds?: string[];
}
interface TestRunInfo {
errs: TestRunErrorFormattableAdapter[];
warnings: string[];
durationMs: number;
unstable: boolean;
screenshotPath: string;
screenshots: unknown;
videos: unknown;
quarantine: unknown;
skipped: boolean;
browsers: unknown[];
testId: string;
}
interface PluginMethodArguments {
initialObject: Task | MessageBus | null;
method: string;
args: unknown[];
}
interface ReportTestActionEventArguments {
command: CommandBase;
duration: number;
result: unknown;
testRun: TestRun;
err: TestRunErrorFormattableAdapter;
}
interface ReportTaskActionEventArguments {
apiActionName: string;
restArgs: object;
}
interface ReportWarningEventArguments {
message: string;
testRun?: TestRun;
actionId?: string;
}
export default class Reporter {
public readonly plugin: ReporterPluginHost;
public readonly messageBus: MessageBus;
public disposed: boolean;
public taskInfo: TaskInfo | null;
public readonly outStream: Writable;
public constructor (plugin: ReporterPlugin, messageBus: MessageBus, outStream: Writable, name: string) {
this.plugin = new ReporterPluginHost(plugin, outStream, name);
this.messageBus = messageBus;
this.disposed = false;
this.taskInfo = null;
this.outStream = outStream;
this._assignMessageBusEventHandlers();
}
private static _isSpecialStream (stream: Writable): boolean {
return (stream as WriteStream).isTTY || stream === process.stdout || stream === process.stderr;
}
private static _createPendingPromise (): PendingPromise {
let resolver = null;
const promise = new Promise(resolve => {
resolver = resolve;
}) as unknown as PendingPromise;
promise.resolve = resolver;
return promise;
}
public async init (): Promise<void> {
await this.dispatchToPlugin({
method: ReporterPluginMethod.init,
initialObject: null,
args: [{}],
});
}
public async dispatchToPlugin ({ method, initialObject, args = [] }: PluginMethodArguments): Promise<void> {
try {
// @ts-ignore
await this.plugin[method](...args);
}
catch (originalError) {
const uncaughtError = new ReporterPluginError({
name: this.plugin.name,
method,
originalError,
});
if (initialObject)
await initialObject.emit('error', uncaughtError);
else
throw uncaughtError;
}
}
private _assignMessageBusEventHandlers (): void {
const messageBus = this.messageBus;
messageBus.on('warning-add', async e => await this._onWarningAddHandler(e));
messageBus.once('start', async (task: Task) => await this._onceTaskStartHandler(task));
messageBus.on('test-run-start', async testRun => await this._onTaskTestRunStartHandler(testRun));
messageBus.on('test-run-done', async testRun => await this._onTaskTestRunDoneHandler(testRun));
messageBus.on('test-action-start', async e => await this._onTaskTestActionStart(e));
messageBus.on('test-action-done', async e => await this._onTaskTestActionDone(e));
messageBus.once('done', async () => await this._onceTaskDoneHandler());
}
public async dispose (): Promise<unknown> {
if (this.disposed)
return Promise.resolve();
this.disposed = true;
if (!this.outStream || Reporter._isSpecialStream(this.outStream) || !isWritableStream(this.outStream))
return Promise.resolve();
const streamFinishedPromise = new Promise(resolve => {
this.outStream.once('finish', resolve);
this.outStream.once('error', resolve);
});
this.outStream.end();
return streamFinishedPromise;
}
private static async _ensureOutStream (outStream: string | WritableStream): Promise<WritableStream> {
if (typeof outStream !== 'string')
return outStream;
const fullReporterOutputPath = resolvePathRelativelyCwd(outStream);
await makeDir(path.dirname(fullReporterOutputPath));
return fs.createWriteStream(fullReporterOutputPath);
}
private static _addDefaultReporter (reporters: ReporterSource[]): void {
reporters.push({
name: 'spec',
output: process.stdout,
});
}
public static async getReporterPlugins (reporters: ReporterSource[] = []): Promise<ReporterPluginSource[]> {
if (!reporters.length)
Reporter._addDefaultReporter(reporters);
return Promise.all(reporters.map(async ({ name, output, options }) => {
const pluginFactory = getPluginFactory(name);
const processedName = processReporterName(name);
const outStream = output ? await Reporter._ensureOutStream(output) : void 0;
return {
plugin: pluginFactory(options),
name: processedName,
outStream,
};
}));
}
private async _onWarningAddHandler ({ message, testRun, actionId }: ReportWarningEventArguments): Promise<void> {
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportWarnings as string,
initialObject: this.messageBus,
args: [
{
message,
testRunId: testRun?.id,
actionId,
},
],
});
}
//Task
private static _createTestItem (test: Test, runsPerTest: number): TestInfo {
return {
fixture: test.fixture as Fixture,
test: test,
testRunIds: [],
screenshotPath: null,
screenshots: [],
videos: [],
quarantine: null,
errs: [],
warnings: [],
unstable: false,
startTime: null,
testRunInfo: null,
pendingRuns: runsPerTest,
pendingStarts: runsPerTest,
pendingTestRunDonePromise: Reporter._createPendingPromise(),
pendingTestRunStartPromise: Reporter._createPendingPromise(),
browsers: [],
};
}
private static _createTestQueue (task: Task): TestInfo[] {
const runsPerTest = task.browserConnectionGroups.length;
return task.tests.map(test => Reporter._createTestItem(test, runsPerTest));
}
private static _createTestRunInfo (reportItem: TestInfo): TestRunInfo {
return {
errs: sortBy(reportItem.errs, ['userAgent', 'code']),
warnings: reportItem.warnings,
durationMs: +new Date() - (reportItem.startTime as number), //eslint-disable-line @typescript-eslint/no-extra-parens
unstable: reportItem.unstable,
screenshotPath: reportItem.screenshotPath as string,
screenshots: reportItem.screenshots,
videos: reportItem.videos,
quarantine: reportItem.quarantine,
skipped: reportItem.test.skip,
browsers: reportItem.browsers,
testId: reportItem.test.id,
};
}
private _getTestItemForTestRun (taskInfo: TaskInfo, testRun: TestRun): TestInfo | undefined {
return find(taskInfo.testQueue, i => i.test === testRun.test);
}
private async _shiftTestQueue (): Promise<void> {
if (!this.taskInfo)
return;
let currentFixture = null;
let nextReportItem = null;
let testItem = null;
const testQueue = this.taskInfo.testQueue;
while (testQueue.length && testQueue[0].testRunInfo) {
testItem = testQueue.shift() as TestInfo;
currentFixture = testItem.fixture;
// NOTE: here we assume that tests are sorted by fixture.
// Therefore, if the next report item has a different
// fixture, we can report this fixture start.
nextReportItem = testQueue[0];
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTestDone,
initialObject: this.taskInfo.task,
args: [
testItem.test.name,
testItem.testRunInfo,
testItem.test.meta,
],
});
if (!nextReportItem)
continue;
if (nextReportItem.fixture === currentFixture)
continue;
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportFixtureStart,
initialObject: this.taskInfo.task,
args: [
nextReportItem.fixture.name,
nextReportItem.fixture.path,
nextReportItem.fixture.meta,
],
});
}
}
private async _resolveTestItem (taskInfo: TaskInfo, testItem: TestInfo, testRun: TestRun): Promise<void> {
if (!taskInfo.task)
return;
if (taskInfo.task.screenshots.hasCapturedFor(testRun.test)) {
testItem.screenshotPath = taskInfo.task.screenshots.getPathFor(testRun.test);
testItem.screenshots = taskInfo.task.screenshots.getScreenshotsInfo(testRun.test);
}
if (taskInfo.task.videos)
testItem.videos = taskInfo.task.videos.getTestVideos(testItem.test.id);
if (testRun.quarantine) {
const testItemQuarantine = testRun.quarantine.attempts.reduce((result: Record<string, object>, { errors }, index: number) => {
const passed = !errors.length;
const quarantineAttempt = index + 1;
result[quarantineAttempt] = { passed };
return result;
}, { });
Object.assign(testItem.quarantine, testItemQuarantine);
}
if (!testItem.testRunInfo) {
testItem.testRunInfo = Reporter._createTestRunInfo(testItem);
if (testItem.test.skip)
taskInfo.skipped++;
else if (testItem.errs.length)
taskInfo.failed++;
else
taskInfo.passed++;
}
await this._shiftTestQueue();
(testItem.pendingTestRunDonePromise.resolve as Function)();
}
private _prepareReportTestActionEventArgs ({ command, duration, result, testRun, err }: ReportTestActionEventArguments): any {
const args: any = {};
if (err)
args.err = err;
if (typeof duration === 'number')
args.duration = duration;
const testFixture = testRun.test.fixture as Fixture;
return Object.assign(args, {
testRunId: testRun.id,
test: {
id: testRun.test.id,
name: testRun.test.name,
phase: testRun.phase,
},
fixture: {
name: testFixture.name,
id: testFixture.id,
},
command: formatCommand(command, result),
browser: testRun.browser,
});
}
private async _onceTaskStartHandler (task: Task): Promise<void> {
this.taskInfo = {
task: task,
passed: 0,
failed: 0,
skipped: 0,
testCount: task.tests.filter(test => !test.skip).length,
testQueue: Reporter._createTestQueue(task),
stopOnFirstFail: task.opts.stopOnFirstFail as boolean,
pendingTaskDonePromise: Reporter._createPendingPromise(),
};
const startTime = task.startTime;
const browserConnectionsInfo = task.browserConnectionGroups.map(group => group[0].connectionInfo);
const first = this.taskInfo.testQueue[0];
const taskProperties = {
configuration: task.opts,
dashboardUrl: task.opts.dashboardUrl,
};
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTaskStart,
initialObject: task,
args: [
startTime,
browserConnectionsInfo,
this.taskInfo.testCount,
task.testStructure,
taskProperties,
],
});
if (first) {
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportFixtureStart,
initialObject: task,
args: [
first.fixture.name,
first.fixture.path,
first.fixture.meta,
],
});
}
}
private async _onTaskTestRunStartHandler (testRun: TestRun): Promise<unknown> {
if (!this.taskInfo)
return void 0;
const testItem = this._getTestItemForTestRun(this.taskInfo, testRun) as TestInfo;
testItem.testRunIds.push(testRun.id);
if (!testItem.startTime)
testItem.startTime = +new Date();
testItem.pendingStarts--;
if (!testItem.pendingStarts) {
// @ts-ignore
if (this.plugin.reportTestStart) {
const testStartInfo = {
testRunIds: testItem.testRunIds,
testId: testItem.test.id,
startTime: new Date(testItem.startTime),
skipped: testItem.test.skip,
};
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTestStart as string,
initialObject: this.taskInfo.task,
args: [
testItem.test.name,
testItem.test.meta,
testStartInfo,
],
});
}
(testItem.pendingTestRunStartPromise.resolve as Function)();
}
return testItem.pendingTestRunStartPromise;
}
private async _onTaskTestRunDoneHandler (testRun: TestRun): Promise<void> {
if (!this.taskInfo)
return;
const reportItem = this._getTestItemForTestRun(this.taskInfo, testRun) as TestInfo;
const isTestRunStoppedTaskExecution = !!testRun.errs.length && this.taskInfo.stopOnFirstFail;
const browser: BrowserInfo = Object.assign({ testRunId: testRun.id }, testRun.browser);
reportItem.browsers.push(browser);
reportItem.pendingRuns = isTestRunStoppedTaskExecution ? 0 : reportItem.pendingRuns - 1;
reportItem.unstable = reportItem.unstable || testRun.unstable;
reportItem.errs = reportItem.errs.concat(testRun.errs);
reportItem.warnings = testRun.warningLog ? union(reportItem.warnings, testRun.warningLog.messages) : [];
if (testRun.quarantine) {
reportItem.quarantine = reportItem.quarantine || {};
const reportItemQuarantine = testRun.quarantine.attempts.reduce((result: Record<string, object>, { errors, testRunId }) => {
const passed = !errors.length;
result[testRunId] = { passed, errors };
browser.quarantineAttemptsTestRunIds = browser.quarantineAttemptsTestRunIds || [];
browser.quarantineAttemptsTestRunIds.push(testRunId);
return result;
}, {});
Object.assign(reportItem.quarantine, reportItemQuarantine);
}
if (!reportItem.pendingRuns)
await this._resolveTestItem(this.taskInfo, reportItem, testRun);
await reportItem.pendingTestRunDonePromise;
}
private async _onTaskTestActionStart ({ apiActionName, ...restArgs }: ReportTaskActionEventArguments): Promise<void> {
if (!this.taskInfo)
return;
// @ts-ignore
if (this.plugin.reportTestActionStart) {
restArgs = this._prepareReportTestActionEventArgs(restArgs as unknown as ReportTestActionEventArguments);
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTestActionStart as string,
initialObject: this.taskInfo.task,
args: [
apiActionName,
restArgs,
],
});
}
}
private async _onTaskTestActionDone ({ apiActionName, ...restArgs }: ReportTaskActionEventArguments): Promise<void> {
if (!this.taskInfo)
return;
// @ts-ignore
if (this.plugin.reportTestActionDone) {
restArgs = this._prepareReportTestActionEventArgs(restArgs as unknown as ReportTestActionEventArguments);
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTestActionDone as string,
initialObject: this.taskInfo.task,
args: [
apiActionName,
restArgs,
],
});
}
}
private async _onceTaskDoneHandler (): Promise<void> {
if (!this.taskInfo)
return;
const endTime = new Date();
const result = {
passedCount: this.taskInfo.passed,
failedCount: this.taskInfo.failed,
skippedCount: this.taskInfo.skipped,
};
await this.dispatchToPlugin({
method: ReporterPluginMethod.reportTaskDone,
initialObject: this.taskInfo.task,
args: [
endTime,
this.taskInfo.passed,
this.taskInfo.task?.warningLog.messages,
result,
],
});
(this.taskInfo.pendingTaskDonePromise.resolve as Function)();
}
}