This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
forked from reportportal/agent-js-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
327 lines (274 loc) · 11 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
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
/*
* Copyright 2020 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const RPClient = require('@reportportal/client-javascript');
const glob = require('glob');
const sanitizeFilename = require('sanitize-filename');
const escapeGlob = require('glob-escape');
const fs = require('fs');
const {
getClientInitObject, getSuiteStartObject,
getStartLaunchObject, getTestStartObject, getStepStartObject,
getAgentInfo, getCodeRef, getFullTestName, getFullStepName,
} = require('./utils/objectUtils');
const getOptions = require('./utils/getOptions');
const testItemStatuses = { PASSED: 'passed', FAILED: 'failed', SKIPPED: 'pending' };
const logLevels = {
ERROR: 'error',
TRACE: 'trace',
DEBUG: 'debug',
INFO: 'info',
WARN: 'warn',
};
const promiseErrorHandler = (promise) => {
promise.catch((err) => {
console.log(err); // suppressed to not affect GitHub actions
console.log('::set-output name=RP_ERROR::true');
});
};
class JestReportPortal {
constructor(globalConfig, options) {
const agentInfo = getAgentInfo();
this.reportOptions = getClientInitObject(getOptions.options(options));
this.client = new RPClient(this.reportOptions, agentInfo);
this.tempSuiteIds = new Map();
this.tempTestIds = new Map();
this.tempStepId = null;
this.promises = [];
this.disabled = this.reportOptions.disabled;
this.disableUploadAttachments = this.reportOptions.disableUploadAttachments;
}
// eslint-disable-next-line no-unused-vars
onRunStart() {
if (this.disabled) return;
const startLaunchObj = getStartLaunchObject(this.reportOptions);
const { tempId, promise } = this.client.startLaunch(startLaunchObj);
this.tempLaunchId = tempId;
promiseErrorHandler(promise);
this.promises.push(promise);
}
// eslint-disable-next-line no-unused-vars
onTestResult(test, testResult) {
if (this.disabled) return;
let suiteDuration = 0;
let testDuration = 0;
for (let result = 0; result < testResult.testResults.length; result++) {
suiteDuration += testResult.testResults[result].duration;
if (testResult.testResults[result].ancestorTitles.length !== 1) {
testDuration += testResult.testResults[result].duration;
}
}
testResult.testResults.forEach((t) => {
if (t.ancestorTitles.length > 0) {
this._startSuite(t.ancestorTitles[0], test.path, suiteDuration);
}
if (t.ancestorTitles.length > 1) {
this._startTest(t, test.path, testDuration);
}
if (parseInt(process.env.DETOX_RERUN_INDEX, 10) > 0) {
this._startStep(t, true, test.path);
this._finishStep(t, true, parseInt(process.env.DETOX_RERUN_INDEX, 10));
return;
}
if (!t.invocations) {
this._startStep(t, false, test.path);
this._finishStep(t, false);
return;
}
for (let i = 0; i < t.invocations; i++) {
const isRetried = t.invocations !== 1;
this._startStep(t, isRetried, test.path);
this._finishStep(t, isRetried, i);
}
});
this.tempTestIds.forEach((tempTestId, key) => {
this._finishTest(tempTestId, key);
});
this.tempSuiteIds.forEach((tempSuiteId, key) => {
this._finishSuite(tempSuiteId, key);
});
}
// eslint-disable-next-line no-unused-vars
async onRunComplete() {
if (this.disabled) return;
await Promise.all(this.promises);
if (this.reportOptions.launchId) return;
const { promise } = this.client.finishLaunch(this.tempLaunchId);
if (this.reportOptions.logLaunchLink === true) {
promise.then((response) => {
console.log(`\nReportPortal Launch Link: ${response.link}`);
console.log(`::set-output name=RP_LINK::${response.link}`);
});
}
promiseErrorHandler(promise);
await promise;
}
_startSuite(suiteName, path, suiteDuration) {
if (this.tempSuiteIds.get(suiteName)) {
return;
}
const codeRef = getCodeRef(path, suiteName);
const { tempId, promise } = this.client.startTestItem(getSuiteStartObject(suiteName, codeRef, suiteDuration),
this.tempLaunchId);
this.tempSuiteIds.set(suiteName, tempId);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_startTest(test, testPath, testDuration) {
if (this.tempTestIds.get(test.ancestorTitles.join('/'))) {
return;
}
const tempSuiteId = this.tempSuiteIds.get(test.ancestorTitles[0]);
const fullTestName = getFullTestName(test);
const codeRef = getCodeRef(testPath, fullTestName);
const testStartObj = getTestStartObject(
test.ancestorTitles[test.ancestorTitles.length - 1], codeRef, testDuration,
);
const parentId = this.tempTestIds.get(test.ancestorTitles.slice(0, -1).join('/')) || tempSuiteId;
const { tempId, promise } = this.client.startTestItem(testStartObj, this.tempLaunchId, parentId);
this.tempTestIds.set(fullTestName, tempId);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_startStep(test, isRetried, testPath) {
const tempSuiteId = this.tempSuiteIds.get(test.ancestorTitles[0]);
const fullStepName = getFullStepName(test);
const codeRef = getCodeRef(testPath, fullStepName);
const stepDuration = test.duration;
const stepStartObj = getStepStartObject(test.title, isRetried, codeRef, stepDuration);
const parentId = this.tempTestIds.get(test.ancestorTitles.join('/')) || tempSuiteId;
const { tempId, promise } = this.client.startTestItem(stepStartObj, this.tempLaunchId, parentId);
this.tempStepId = tempId;
promiseErrorHandler(promise);
this.promises.push(promise);
}
_finishStep(test, isRetried, invocation) {
const errorMsg = test.failureMessages[0];
switch (test.status) {
case testItemStatuses.PASSED:
this._finishPassedStep(isRetried);
break;
case testItemStatuses.FAILED:
this._finishFailedStep(errorMsg, isRetried, test, invocation);
break;
default:
this._finishSkippedStep(isRetried);
}
}
_finishPassedStep(isRetried) {
const status = testItemStatuses.PASSED;
const finishTestObj = { status, retry: isRetried };
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_finishFailedStep(failureMessage, isRetried, test, invocation) {
const status = testItemStatuses.FAILED;
const finishTestObj = { status, retry: isRetried };
this._sendLog(failureMessage, test, invocation);
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_sendLog(message, test, invocation) {
const logObject = {
message,
level: logLevels.ERROR,
};
const { promise } = this.client.sendLog(this.tempStepId, logObject);
promiseErrorHandler(promise);
this.promises.push(promise);
if (this.disableUploadAttachments) return;
const attachments = this._getAttachments(test, invocation);
if (attachments.length > 0) {
// add attachments to test log
attachments.forEach((attachment) => {
const logObject = {
message: `Attachment: ${attachment.name}`,
level: logLevels.DEBUG,
};
const { promise } = this.client.sendLog(this.tempStepId, logObject, attachment);
promiseErrorHandler(promise);
this.promises.push(promise);
});
}
}
_getAttachments(test, invocation) {
if (!test) {
return [];
}
const attachments = [];
let testName = test.fullName;
let filenamePrefix = '';
if (invocation > 0) {
testName = `${testName} (${invocation + 1})`;
filenamePrefix = `Retry ${invocation + 1} - `;
}
// eslint-disable-next-line max-len
const files = glob.sync(`${this.reportOptions.artifactsPath}/*/*+(${this._convertTestNameToDirectory(testName)})/*.?(png|log|mp4)`);
files.forEach((path) => {
const filename = filenamePrefix + path.replace(/^.*[\\\/]/, '');
const extension = filename.split('.').pop();
let mimetype;
switch (extension) {
case 'mp4':
mimetype = 'video/mp4';
break;
case 'png':
mimetype = 'image/png';
break;
default:
mimetype = 'text/plain';
}
attachments.push({
name: filename,
type: mimetype,
content: fs.readFileSync(path),
});
});
return attachments;
}
_convertTestNameToDirectory(testName) {
const SANITIZE_OPTIONS = { replacement: '_' };
return escapeGlob(sanitizeFilename(testName, SANITIZE_OPTIONS));
}
_finishSkippedStep(isRetried) {
const status = 'skipped';
const issue = this.reportOptions.skippedIssue === false ? { issueType: 'NOT_ISSUE' } : null;
const finishTestObj = {
status,
retry: isRetried,
...(issue && { issue }),
};
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_finishTest(tempTestId, key) {
if (!tempTestId) return;
const { promise } = this.client.finishTestItem(tempTestId, {});
this.tempTestIds.delete(key);
promiseErrorHandler(promise);
this.promises.push(promise);
}
_finishSuite(tempSuiteId, key) {
if (!tempSuiteId) return;
const { promise } = this.client.finishTestItem(tempSuiteId, {});
this.tempSuiteIds.delete(key);
promiseErrorHandler(promise);
this.promises.push(promise);
}
}
module.exports = JestReportPortal;