-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
executable file
·177 lines (158 loc) · 4.43 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
var Base = require("mocha").reporters.Base;
var postTestData = require('./src/elastic-search');
exports = module.exports = ELKReporter;
/**
* Initialize a new `ELK` reporter.
*
* @api public
* @param {Runner} runner
*/
function ELKReporter(runner) {
Base.call(this, runner);
var self = this;
var tests = [];
var pending = [];
var failures = [];
var passes = [];
var passesCount = 0;
var failuresCount = 0;
var pendingCount = 0;
runner.on('test end', function(test) {
tests.push(test);
});
runner.on('pass', function(test) {
passesCount++;
passes.push(test);
});
runner.on('fail', function(test) {
failuresCount++;
failures.push(test);
});
runner.on('pending', function(test) {
pendingCount++;
pending.push(test);
});
runner.on('end', function() {
if (failures.length) {
let failuresToIndex = new Map();
failures.forEach((failedTest, index) => {
if (!failedTest.context) {
failuresToIndex.set(failedTest.title, index);
}
});
if (failuresToIndex.size) {
tests.forEach(test => {
if (failuresToIndex.has(test.title)) {
const indexToReplace = failuresToIndex.get(test.title);
failures[indexToReplace] = test;
}
});
}
}
// add failed retry attempts to failures array
addRetryFailures(failures, passes);
var obj = {
stats: self.stats,
tests: tests.map(clean),
pending: pending.map(clean),
failures: failures.map(clean),
passes: passes.map(clean)
};
console.log('\n' + passesCount + ' passed','\n' + failuresCount + ' failed','\n' + pendingCount + ' pending','\n');
runner.testResults = obj;
postTestData(obj, function(err) {
if(err) {
console.log("********* ERROR GENERATED BY ELK-REPORTER *********");
if(err.severity && err.severity === "high") {
console.log("** SEVERE ERROR **");
process.exit(-1);
} else {
console.log("** Non-severe error **");
}
console.log(err);
}
});
});
}
/**
* Adds previously failed retry attempts to failures
*
* @param {Object} failures
* @param {Object} passes
*/
function addRetryFailures(failures, passes) {
let prevAttempts = [];
// when test fails in all attempts
failures.forEach(failure => {
if (failure.prevAttempts && failure.prevAttempts.length) {
failure.prevAttempts.forEach(failedTestAttempt => {
if (!failedTestAttempt.fullTitle) {
addRequiredProps(failedTestAttempt, failure, prevAttempts)
} else {
prevAttempts.push(failedTestAttempt);
}
})
}
});
// when test passes but has failed attempts
passes.forEach(passedTest => {
if (passedTest.prevAttempts && passedTest.prevAttempts.length) {
passedTest.prevAttempts.forEach(failedTestAttempt => {
if (!failedTestAttempt.fullTitle) {
addRequiredProps(failedTestAttempt, passedTest, prevAttempts)
} else {
prevAttempts.push(failedTestAttempt);
}
})
}
});
if (prevAttempts.length) {
failures.push(...prevAttempts);
}
}
/**
* Adds required properties from parent test instance to previously failed attempt
*
* @param {Object} testAttempt - test instance from prevAttempts
* @param {Object} parentTestInstance - parent test instance with property prevAttempts
* @param {Array} prevAttempts - array to insert all previous failures
*/
function addRequiredProps(failedAttempt, parentTestInstance, prevAttempts) {
failedAttempt.context = parentTestInstance.context;
failedAttempt.fullTitle = parentTestInstance.fullTitle;
failedAttempt.parent = parentTestInstance.parent;
failedAttempt.title = parentTestInstance.title;
failedAttempt.titlePath = parentTestInstance.titlePath;
prevAttempts.push(failedAttempt);
}
/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @api private
* @param {Object} test
* @return {Object}
*/
function clean(test) {
return {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
err: errorJSON(test.err || {}),
context: test.context
};
}
/**
* Transform `error` into a JSON object.
*
* @api private
* @param {Error} err
* @return {Object}
*/
function errorJSON(err) {
var res = {};
Object.getOwnPropertyNames(err).forEach(function(key) {
res[key] = err[key];
}, err);
return res;
}