-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
268 lines (234 loc) · 6.75 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
/*
** based on https://github.com/mocha-community/json-file-reporter/blob/master/src/index.js
*/
const _ = require('lodash');
const fs = require('fs')
const path = require('path')
const Mocha = require('mocha')
const debug = require('debug')('mocha-json-reporter');
const md5 = require('md5');
const {
EVENT_TEST_PASS,
EVENT_TEST_FAIL,
EVENT_TEST_END,
EVENT_TEST_BEGIN,
EVENT_RUN_END,
EVENT_TEST_PENDING,
EVENT_SUITE_END,
EVENT_SUITE_BEGIN
} = Mocha.Runner.constants
const DEFAULT_REPORT_PATH = 'report-[hash].json'
function MochaJsonReporter(runner, options) {
//console.log(JSON.stringify(options));
if (options.reporterOptions.enabled === false) return;
const minimal = (options.reporterOptions.minimal === true);
Mocha.reporters.Base.call(this, runner, options)
const self = this
const x = {};
runner.on(EVENT_TEST_END, function (test) {
const key = suiteTitle(test.suite);
x[key].tests.push(test);
// for(let i = 0;i<x[key].tests.length;i++){
// const item = x[key].tests[i];
// if(item.fullTitle() === test.fullTitle()) {
// x[key].tests[i] = test;
// // break;
// }
// }
})
runner.on(EVENT_TEST_PASS, function (test) {
const key = suiteTitle(test.suite);
x[key].passes.push(test)
})
//runner.on(EVENT_TEST_BEGIN, function (test) {
// const key = suiteTitle(test.suite);
// x[key].passes.push(test)
//})
runner.on(EVENT_TEST_FAIL, function (test) {
const key = suiteTitle(test.suite);
x[key].failures.push(test)
})
runner.on(EVENT_TEST_PENDING, function (test) {
const key = suiteTitle(test.suite);
x[key].pending.push(test)
})
runner.on(EVENT_SUITE_BEGIN, function (suite) {
const key = suiteTitle(suite);
x[key] = {
tests: [],
pending: [],
failures: [],
passes: [],
};
suite.tests.forEach(test => {
x[key].tests.push({
title: test.title,
fullTitle: () => test.fullTitle(),
state: 'skipped',
fileName: test.invocationDetails?.relativeFile ?? '',
testConfig: testConfigList(test),
duration: test.duration,
currentRetry: () => test.currentRetry(),
err: '',
});
});
// suite.tests.forEach(test=>{
// var err = test.err || {}
// if (err instanceof Error) {
// err = errorJSON(err)
// }
// x[key].tests.push({
// title: test.title,
// fullTitle: () => test.fullTitle() ,
// state:'skipped',
// fileName: test.invocationDetails?.relativeFile?? '',
// testConfig: testConfigList(test),
// duration: test.duration,
// currentRetry: () => test.currentRetry(),
// err: cleanCycles(err),
// });
// })
})
runner.on(EVENT_SUITE_END, function (suite) {
const key = suiteTitle(suite);
const obj = {
stats: self.stats,
tests: x[key].tests.map(clean),
pending: x[key].pending.map(clean),
failures: x[key].failures.map(clean),
passes: x[key].passes.map(clean)
}
runner.testResults = obj;
if (obj.pending.length === 0 && obj.passes.length === 0 && obj.failures.length === 0) {
//TODO
return;
}
const json = JSON.stringify(obj, null, minimal ? 0 : 2)
let fn = DEFAULT_REPORT_PATH
const { reporterOptions } = options
if (reporterOptions) {
const { output } = reporterOptions
if (output) {
fn = output
}
}
writeJson(json, fn);
})
runner.once(EVENT_RUN_END, function () {
})
}
function suiteTitle(suite) {
let s = suite;
let k = '';
while (s && s.root === false) {
k = ''.concat(s.title, k);
s = s.parent;
}
return k === '' ? 'root' : k.trim();
}
function testConfigList(test) {
let x = {}, y = {};
//console.log(JSON.stringify(test._testConfig.testConfigList));
let overrides = test._testConfig && test._testConfig.testConfigList ? test._testConfig.testConfigList.map(e => e.overrides) : [];
overrides.forEach(override => {
if (override && override.env) {
let e = override.env;
for (const key in e) {
if (Object.hasOwnProperty.call(e, key)) {
const element = e[key];
if (Object.hasOwnProperty.call(x, key)) {
let arr = x[key];
let val = Array.isArray(element) ? [...element] : [element];
val.forEach(v => {
arr.push(v);
})
} else {
x[key] = Array.isArray(element) ? [...element] : [element];
}
}
}
// console.log(JSON.stringify(override));
}
});
for (const key in x) {
if (Object.hasOwnProperty.call(x, key)) {
const element = x[key];
y[key] = _.uniq(element);
}
}
return y;
}
function clean(test) {
var err = test.err || {}
if (err instanceof Error) {
err = errorJSON(err)
}
return {
title: test.title,
fullTitle: test.fullTitle(),
fileName: test.invocationDetails?.relativeFile ?? '',
state: test.state,
duration: test.duration,
currentRetry: test.currentRetry(),
err: cleanCycles(err),
testConfig: testConfigList(test)
}
}
function cleanCycles(obj) {
const cache = []
return JSON.parse(
JSON.stringify(obj, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return '' + value
}
cache.push(value)
}
return value
})
)
}
/**
* Writes a JUnit test report XML document.
* @param {string} xml - xml string
* @param {string} filePath - path to output file
*/
function writeJson(json, filePath) {
function fn2X(doc) {
const s2 = `cypress/e2e/`;
var def = 'result';
if (doc && doc.tests && doc.tests.length > 0) {
var s4 = doc.tests[0].fileName ?? def;
if (s4.includes(`\\`)) { s4 = s4.replace(/\\/g, '/'); }
if (s4.includes(s2)) { s4 = s4.replace(/cypress\/e2e\//g, ''); }
if (s4.includes(`.cy.js`)) { s4 = s4.replace(/\.cy\.js/g, ''); }
if (s4.includes(`.cy.ts`)) { s4 = s4.replace(/\.cy\.ts/g, ''); }
return s4;
}
return def;
}
if (filePath) {
if (filePath.indexOf('[hash]') !== -1) {
filePath = filePath.replace('[hash]', md5(json));
}
if (filePath.indexOf('[spec]') !== -1) {
filePath = filePath.replace('[spec]', fn2X(JSON.parse(json)));
}
//console.info('writing json file to', filePath);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
try {
fs.writeFileSync(filePath, json, 'utf-8');
} catch (exc) {
debug('problem writing results: ' + exc);
}
debug('results written successfully');
}
};
function errorJSON(err) {
const res = {}
Object.getOwnPropertyNames(err).forEach(function (key) {
res[key] = err[key]
}, err)
return res
}
module.exports = MochaJsonReporter;