/
index.js
250 lines (192 loc) · 8.8 KB
/
index.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
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
import { find, sortBy, union } from 'lodash';
import { writable as isWritableStream } from 'is-stream';
import ReporterPluginHost from './plugin-host';
import { CommandReportItem } from './command-report-item';
import TestCafeErrorList from '../errors/error-list';
export default class Reporter {
constructor (plugin, task, outStream) {
this.plugin = new ReporterPluginHost(plugin, outStream);
this.task = task;
this.disposed = false;
this.passed = 0;
this.failed = 0;
this.skipped = 0;
this.testCount = task.tests.filter(test => !test.skip).length;
this.reportQueue = Reporter._createReportQueue(task);
this.stopOnFirstFail = task.opts.stopOnFirstFail;
this.outStream = outStream;
this.pendingTaskDonePromise = Reporter._createPendingPromise();
this._assignTaskEventHandlers();
}
static _isSpecialStream (stream) {
return stream.isTTY || stream === process.stdout || stream === process.stderr;
}
static _createPendingPromise () {
let resolver = null;
const promise = new Promise(resolve => {
resolver = resolve;
});
promise.resolve = resolver;
return promise;
}
static _createReportItem (test, runsPerTest) {
return {
fixture: test.fixture,
test: test,
screenshotPath: null,
screenshots: [],
quarantine: null,
errs: [],
warnings: [],
unstable: false,
startTime: null,
testRunInfo: null,
pendingRuns: runsPerTest,
pendingStarts: runsPerTest,
pendingTestRunDonePromise: Reporter._createPendingPromise(),
pendingTestRunStartPromise: Reporter._createPendingPromise()
};
}
static _createReportQueue (task) {
const runsPerTest = task.browserConnectionGroups.length;
return task.tests.map(test => Reporter._createReportItem(test, runsPerTest));
}
static _createTestRunInfo (reportItem) {
return {
errs: sortBy(reportItem.errs, ['userAgent', 'code']),
warnings: reportItem.warnings,
durationMs: new Date() - reportItem.startTime,
unstable: reportItem.unstable,
screenshotPath: reportItem.screenshotPath,
screenshots: reportItem.screenshots,
quarantine: reportItem.quarantine,
skipped: reportItem.test.skip
};
}
_getReportItemForTestRun (testRun) {
return find(this.reportQueue, i => i.test === testRun.test);
}
async _shiftReportQueue (reportItem) {
let currentFixture = null;
let nextReportItem = null;
while (this.reportQueue.length && this.reportQueue[0].testRunInfo) {
reportItem = this.reportQueue.shift();
currentFixture = reportItem.fixture;
await this.plugin.reportTestDone(reportItem.test.name, reportItem.testRunInfo, reportItem.test.meta);
// 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 = this.reportQueue[0];
if (nextReportItem && nextReportItem.fixture !== currentFixture)
await this.plugin.reportFixtureStart(nextReportItem.fixture.name, nextReportItem.fixture.path, nextReportItem.fixture.meta);
}
}
async _resolveReportItem (reportItem, testRun) {
if (this.task.screenshots.hasCapturedFor(testRun.test)) {
reportItem.screenshotPath = this.task.screenshots.getPathFor(testRun.test);
reportItem.screenshots = this.task.screenshots.getScreenshotsInfo(testRun.test);
}
if (testRun.quarantine) {
reportItem.quarantine = testRun.quarantine.attempts.reduce((result, errors, index) => {
const passed = !errors.length;
const quarantineAttempt = index + 1;
result[quarantineAttempt] = { passed };
return result;
}, {});
}
if (!reportItem.testRunInfo) {
reportItem.testRunInfo = Reporter._createTestRunInfo(reportItem);
if (reportItem.test.skip)
this.skipped++;
else if (reportItem.errs.length)
this.failed++;
else
this.passed++;
}
await this._shiftReportQueue(reportItem);
reportItem.pendingTestRunDonePromise.resolve();
}
_prepareReportTestActionEventArgs ({ command, testRun, errors }) {
const args = {};
if (errors) {
errors = errors instanceof TestCafeErrorList ? errors.items : [errors];
args.errors = errors;
}
return Object.assign(args, {
test: {
name: testRun.test.name,
phase: testRun.phase
},
command: new CommandReportItem(command),
browser: testRun.controller.browser,
});
}
_assignTaskEventHandlers () {
const task = this.task;
task.once('start', async () => {
const startTime = new Date();
const userAgents = task.browserConnectionGroups.map(group => group[0].userAgent);
const first = this.reportQueue[0];
await this.plugin.reportTaskStart(startTime, userAgents, this.testCount);
await this.plugin.reportFixtureStart(first.fixture.name, first.fixture.path, first.fixture.meta);
});
task.on('test-run-start', async testRun => {
const reportItem = this._getReportItemForTestRun(testRun);
if (!reportItem.startTime)
reportItem.startTime = new Date();
reportItem.pendingStarts--;
if (!reportItem.pendingStarts) {
if (this.plugin.reportTestStart)
await this.plugin.reportTestStart(reportItem.test.name, reportItem.test.meta);
reportItem.pendingTestRunStartPromise.resolve();
}
return reportItem.pendingTestRunStartPromise;
});
task.on('test-run-done', async testRun => {
const reportItem = this._getReportItemForTestRun(testRun);
const isTestRunStoppedTaskExecution = !!testRun.errs.length && this.stopOnFirstFail;
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 (!reportItem.pendingRuns)
await this._resolveReportItem(reportItem, testRun);
await reportItem.pendingTestRunDonePromise;
});
task.on('test-action-start', async ({ apiActionName, command, testRun }) => {
if (this.plugin.reportTestActionStart) {
const args = this._prepareReportTestActionEventArgs({ command, testRun });
await this.plugin.reportTestActionStart(apiActionName, args);
}
});
task.on('test-action-done', async ({ apiActionName, command, testRun, errors }) => {
if (this.plugin.reportTestActionDone) {
const args = this._prepareReportTestActionEventArgs({ command, testRun, errors });
await this.plugin.reportTestActionDone(apiActionName, args);
}
});
task.once('done', async () => {
const endTime = new Date();
const result = {
passedCount: this.passed,
failedCount: this.failed,
skippedCount: this.skipped
};
await this.plugin.reportTaskDone(endTime, this.passed, task.warningLog.messages, result);
this.pendingTaskDonePromise.resolve();
});
}
async dispose () {
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;
}
}