-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperAggregate.js
executable file
·127 lines (110 loc) · 3.88 KB
/
superAggregate.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
/**
* TODO (this can be part of another script that we use which is not part of any solution shared with mochawsome):
* - add the profile to the file name
* - use profile in file name to create groups of reports — run the below steps on each profile group
* - then you can keep the aggregate profile reports separate or merge them all together into a super report
*/
const path = require('path')
const fs = require('fs')
const marge = require('mochawesome-report-generator')
module.exports.byProfile = function byProfile(relPathToReports) {
const absPathToReports = path.resolve(relPathToReports)
const reports = fs.readdirSync(absPathToReports).filter((fileName) => {
return fileName.includes('-report.json')
}).map((fileName) => {
return {
profile: fileName.split('-report.json')[0],
report: require(absPathToReports + '/' + fileName),
}
})
if (reports.length === 0) {
return console.warn('No matching reports found to aggregate.')
}
/**
* Step 1:
* - is start time before — change it
* - end time later — change it
* - += duration
*/
let aggReport = reports.reduce((aggReport, report, i) => {
if (i === 0) {
// at the very beginning make the first report the agg report
return report.report
}
aggReport.stats.start = new Date(Math.min(
Date.parse(aggReport.stats.start),
Date.parse(report.report.stats.start)
))
aggReport.stats.end = new Date(Math.max(
Date.parse(aggReport.stats.end),
Date.parse(report.report.stats.end)
))
aggReport.stats.duration += report.report.stats.duration
return aggReport
}, {})
aggReport.suites.suites.forEach((suite) => {
suite.title = '[' + reports[0].profile + '] ' + suite.title
suite.tests.forEach((test) => {
test.title = '[' + reports[0].profile + '] ' + test.title
})
})
/**
* Step 2:
* - prepend profile name to suites and tests titles
* - concat everything together
* - if pass push to allPasses or to allFailures
*/
// TODO this slice is confusing and terrible
aggReport = reports.slice(1).reduce((aggReport, report) => {
// TODO this bad code
report.report.suites.suites.forEach((suite) => {
suite.title = '[' + report.profile + '] ' + suite.title
suite.tests.forEach((test) => {
test.title = '[' + report.profile + '] ' + test.title
})
})
aggReport.suites.suites = aggReport.suites.suites.concat(report.report.suites.suites) // ... lol
aggReport.allTests = aggReport.allTests.concat(report.report.allTests)
aggReport.allPasses = aggReport.allPasses.concat(report.report.allPasses)
aggReport.allFailures = aggReport.allFailures.concat(report.report.allFailures)
return aggReport
}, aggReport)
/**
* Step 3:
* - total run suites
* - total tests run
* - total passing
* - total failures
* - pass percent passed/total registered
* - skipped
* - hasSkipped
*/
aggReport.stats.suites = aggReport.suites.suites.reduce((runSuiteCount, suite) => {
return suiteWasRun(suite) ? runSuiteCount + 1 : runSuiteCount
}, 0)
aggReport.stats.tests = aggReport.allTests.length
aggReport.stats.passes = aggReport.allPasses.length
aggReport.stats.failures = aggReport.allFailures.length
aggReport.stats.passPercent = (aggReport.stats.passes / aggReport.stats.testsRegistered) * 100
aggReport.stats.skipped = aggReport.stats.testsRegistered - aggReport.stats.tests
aggReport.stats.hasSkipped = aggReport.stats.skipped !== 0
/**
* Step 4:
* - save .json report
* - save .html file
*/
fs.writeFileSync(absPathToReports + '/aggregate-report.json', JSON.stringify(aggReport))
marge.create(aggReport, {
reportDir: relPathToReports,
reportFilename: 'aggregate-report',
})
}
/**
* Utils:
*/
function suiteWasRun(suite) {
if (!suite) {
console.log(suite);
}
return suite.totalTests !== suite.totalSkipped
}