This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
346 lines (287 loc) · 10.4 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const mocha = require('mocha')
const path = require('path')
const fs = require('fs')
const QTestClient = require('./qTestClient')
let testSuiteId = process.env.QTEST_SUITE_ID
const buildUrl = process.env.QTEST_BUILD_URL
const parentType = process.env.QTEST_PARENT_TYPE
const parentId = process.env.QTEST_PARENT_ID
const testSuiteName = process.env.QTEST_SUITE_NAME
const createTestRuns = process.env.QTEST_CREATE_TEST_RUNS || true
let printReportUrl = false
/**
* Initialize a new `qTest` reporter.
*
* @param {Runner} runner
* @param {options} options
* @api public
*/
function qTest(runner, options = {}) {
let getSuite, qTestClient
let mapping = []
const qTestConfig = getQTestConfig(options)
const stateFailed = qTestConfig.stateFailed || 'FAIL'
const statePassed = qTestConfig.statePassed || 'PASS'
const statePending = qTestConfig.statePending || 'PENDING'
if (!testSuiteId && (!parentId || !testSuiteName || !parentType)) {
if (!qTestConfig.hideWarning) {
console.warn("qTestReporter: results won't be published.",
'Please set either existing QTEST_SUITE_ID or combination of QTEST_PARENT_TYPE, QTEST_PARENT_ID and QTEST_SUITE_NAME to be created.')
}
} else if (!qTestConfig.host || !qTestConfig.bearerToken || !qTestConfig.projectId) {
console.error("qTestReporter: results won't be published.",
'host, bearerToken, projectId are required options.')
} else {
qTestClient = new QTestClient(qTestConfig.host, qTestConfig.bearerToken, qTestConfig.projectId)
if (testSuiteId) {
// use existing suite
console.log('qTestReporter: getting test runs of test suite', testSuiteId)
getSuite = qTestClient.getSuiteTestRuns(testSuiteId).then(result => {
mapping = result
printReportUrl = true
console.log('qTestReporter: test runs found', Object.keys(result).length, '\n')
})
} else {
// create new suite
console.log('qTestReporter: creating test suite', testSuiteName)
getSuite = qTestClient.createTestSuite(parentType, parentId, testSuiteName).then(result => {
testSuiteId = result
printReportUrl = true
console.log('qTestReporter: test suite created', testSuiteId)
})
}
getSuite.catch(err => {
qTestClient = null
console.log('qTestReporter: failed to get/create test suite:', testSuiteId || testSuiteName)
console.error(err.message)
})
}
const log = setupLogger(qTestConfig)
let startDate = new Date()
let testResults = []
const events = {
onRunnerStart() {
startDate = new Date()
log(0, '\nTests execution started...\n')
},
onTestStart(test) {
log(log.TEST_PAD, `> ${test.title}`)
// get qTestCaseId from test title
const testCaseId = getQTestId(test.title)
if (!testCaseId) {
return console.log('qTestReporter: test is not mapped to qTest')
}
if (!qTestClient) { return }
test.qTest = addTest(test.title, testCaseId, buildUrl)
testResults.push(test.qTest)
},
onTestPass(test) {
log(log.TEST_PAD, `\x1b[1m\x1b[32m✓ PASSED`, '\x1b[0m', durationMsg(test.duration))
log(1)
if (!qTestClient || !test.qTest) { return }
test.qTest.executionLog.status = statePassed
},
onTestFail(test, err = {}) {
log(log.TEST_PAD, `\x1b[1m\x1b[31m\x1b[1mx FAILED`, '\x1b[0m', durationMsg(test.duration))
log(log.TEST_PAD + 2, '\x1b[31m', err.stack, '\x1b[0m')
log(1)
if (!qTestClient || !test.qTest) { return }
test.qTest.executionLog.status = stateFailed
test.qTest.executionLog.note = err.stack
// test.qTest.executionLog.attachments: [{
// name: 'screenshot.png',
// content_type: 'image/png',
// data: 'base64 string of Sample.docx'
// }]
},
onTestSkip(test) {
log(log.TEST_PAD, '\x1b[2m', `- PENDING: ${test.title}`, '\x1b[0m')
log(1)
if (!qTestClient) { return }
if (!test.qTest) {
const testCaseId = getQTestId(test.title)
if (!testCaseId) { return }
test.qTest = addTest(test.title, testCaseId, buildUrl)
testResults.push(test.qTest)
}
test.qTest.executionLog.status = statePending
test.qTest.executionLog.exe_end_date = new Date().toISOString()
},
onTestEnd(test) {
if (!qTestClient || !test.qTest) { return }
test.qTest.executionLog.exe_end_date = new Date().toISOString()
if (!test.qTest.executionLog.status) {
test.qTest.executionLog.status = statePending
}
},
onSuiteStart(suite) {
if (suite.root) { return }
if (suite.parent && suite.parent.root) {
suite.startDate = new Date()
log(0, suite.title)
} else {
log(1, suite.title)
}
},
onSuiteEnd(suite) {
if (suite.root || !suite.parent || !suite.parent.root) { return }
log(0, 'SUITE END', durationMsg(new Date() - suite.startDate), '\n')
testResults = [...testResults, ...getNotStartedTests(suite.parent, stateFailed, statePending)]
},
onHookStart(hook) {
if (hook.title.includes('Global')) { return }
log(log.HOOK_PAD, `~ ${hook.title}`)
},
onHookEnd(hook) {
if (hook.title.includes('Global')) { return }
log(log.HOOK_PAD, `~ DONE`, durationMsg(hook.duration))
},
async onRunnerEnd() {
log(0, '\nTests execution finished.', durationMsg(new Date() - startDate))
if (!qTestClient) { return }
// submit first passed, then pending and failed tests in the end
// to avoid marking skipped or failed tests as passed
testResults
.sort((a, b) => a.executionLog.status === statePending ? -1 : 1)
.sort((a, b) => a.executionLog.status === stateFailed ? 1 : -1)
try {
await getSuite
} catch (err) {
return console.error('qTestReporter: unable to publish results.')
}
console.log('qTestReporter: publishing results...')
for (let i = 0; i < testResults.length; i++) {
const { executionLog, testCaseId, testTitle } = testResults[i]
let testRun
if (mapping[testCaseId]) {
// using existing test run
testRun = mapping[testCaseId]
} else if (createTestRuns) {
// creating new test run
testRun = await qTestClient.createTestRun(testSuiteId, testCaseId)
mapping[testCaseId] = testRun
}
if (testRun) {
const idsLog = `testRunId: '${testRun.id}', testCaseId: '${testCaseId}'`
const logBody = Object.assign({},
executionLog,
{
name: testRun.name,
automation_content: idsLog,
note: `${testTitle} \n${idsLog}` + (executionLog.note ? `\n\r\n ${executionLog.note}` : '')
})
await qTestClient.postLog(testRun.id, logBody)
}
}
},
mochaQTestMappingReporterExit() {
printReportUrl && console.log('\nResults submitted to qTest:',
`\x1b[4m\nhttps://${qTestConfig.host}/p/${qTestConfig.projectId}/portal/project#tab=testexecution&object=2&id=${testSuiteId}\x1b[0m`)
}
}
// if runner is not passed just return events to allow user map them to desired events in their framework
if (runner) {
runner.on('start', events.onRunnerStart)
runner.on('test', events.onTestStart)
runner.on('pass', events.onTestPass)
runner.on('fail', events.onTestFail)
runner.on('pending', events.onTestSkip)
runner.on('test end', events.onTestEnd)
runner.on('suite', events.onSuiteStart)
runner.on('suite end', events.onSuiteEnd)
runner.on('hook', events.onHookStart)
runner.on('hook end', events.onHookEnd)
runner.on('end', events.onRunnerEnd)
// hack for Cypress to avoid final message duplication
if (!qTestConfig.hideResultUrl && !process.listeners('exit').some(evt => evt.name === 'mochaQTestMappingReporterExit')) {
process.on('exit', events.mochaQTestMappingReporterExit)
}
mocha.reporters.Base.call(this, runner)
}
return events
}
function getQTestId(title) {
const match = title.match(/@qTest\[(.*?)\]/)
return match ? match[1] : null
}
function durationMsg(duration = '?') {
return `(${duration}ms)`
}
function setupLogger(qTestConfig) {
const log = qTestConfig.enableLogs !== false ? (pad = 0, ...args) => {
console.log((pad ? '|' : '') + (' '.repeat(pad)), ...args)
} : () => { }
log.TEST_PAD = 5
log.HOOK_PAD = 4
return log
}
function getQTestConfig(options) {
const configFile = (options.reporterOptions && options.reporterOptions.configFile) || options.configFile
let qTestConfig
if (configFile) {
const pathToConfig = path.join(process.cwd(), configFile)
if (fs.existsSync(pathToConfig)) {
qTestConfig = require(pathToConfig)
} else {
console.error('qTestReporter: config file doesn\'t exist.')
}
} else {
qTestConfig = (options.reporterOptions && options.reporterOptions.configOptions) || options
}
return qTestConfig || {}
}
/**
* get not started tests due to failure of before/beforeEach hook
* @param {Mocha.Suite} suite
* @param {string} stateFailed
* @param {string} statePending
* @returns {Array}
*/
function getNotStartedTests(suite, stateFailed, statePending) {
let tests = []
if (suite.tests) {
for (let i = 0; i < suite.tests.length; i++) {
let test = suite.tests[i]
if (test.qTest || test.state) continue
const testCaseId = getQTestId(test.title)
if (!testCaseId) continue
test.qTest = {
executionLog: {
build_url: buildUrl,
exe_start_date: new Date().toISOString(),
exe_end_date: new Date().toISOString()
},
testCaseId,
testTitle: test.title
}
if (test.pending) {
test.qTest.executionLog.status = statePending
test.qTest.executionLog.note = 'Test or its suite is skipped.'
} else {
test.qTest.executionLog.status = stateFailed
test.qTest.executionLog.note = 'Test was not started and marked as failed due to failure in before/beforeEach hook.'
}
tests.push(test.qTest)
}
}
if (suite.suites) {
suite.suites.forEach(suite => {
tests = [...tests, ...getNotStartedTests(suite, stateFailed, statePending)]
})
}
return tests
}
function addTest(testTitle, testCaseId, buildUrl) {
return {
executionLog: {
build_url: buildUrl,
exe_start_date: new Date().toISOString()
},
testCaseId,
testTitle
}
}
/**
* Expose `Mocha qTest Mapping Reporter`.
*/
module.exports = qTest