forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-reporter.js
159 lines (134 loc) · 3.76 KB
/
log-reporter.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
'use strict';
const Mocha = require('mocha');
const { EVENT_TEST_END, EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS } = Mocha.Runner.constants;
class LogReporter extends Mocha.reporters.Spec {
constructor(runner, options = {}) {
super(runner, options);
this._testsResults = [];
this._testFailures = [];
this._testPasses = [];
runner.on(EVENT_TEST_END, (test) => {
this._testsResults.push(test);
});
runner.on(EVENT_TEST_PASS, (test) => {
this._testPasses.push(test);
});
runner.on(EVENT_TEST_FAIL, (test) => {
this._testFailures.push(test);
});
runner.once(EVENT_RUN_END, () => {
this.reportStats();
this.reportResults();
this.reportErrors();
});
}
reportStats() {
const stats = {
...this.stats,
// Format time in epoch
start: this.stats.start.getTime(),
end: this.stats.end.getTime(),
};
// Example
// CypressStats suites=1 tests=2 testPasses=1 pending=0 failures=1
// start=1668783563731 end=1668783645198 duration=81467
console.log(`CypressStats ${objToLogAttributes(stats)}`);
}
reportResults() {
this._testsResults.map(cleanTest).map((test) => {
// Example
// CypressTestResult title="Login scenario, create test data source, dashboard, panel, and export scenario"
// suite="Smoke tests" file=../../e2e/smoke-tests-suite/1-smoketests.spec.ts duration=68694
// currentRetry=0 speed=undefined err=false
console.log(`CypressTestResult ${objToLogAttributes(test)}`);
});
}
reportErrors() {
this._testFailures.map(cleanTest).forEach((failure) => {
const suite = failure.suite;
const test = failure.title;
const error = failure.err;
// Example
// CypressError suite="Smoke tests" test="Login scenario, create test data source, dashboard,
// panel, and export scenario" error=false
console.error(`CypressError ${objToLogAttributes({ suite, test, error })}`);
});
}
}
/**
* Stringify object to be log friendly
* @param {Object} obj
* @returns {String}
*/
function objToLogAttributes(obj) {
return Object.entries(obj)
.map(([key, value]) => `${key}=${formatValue(value)}`)
.join(' ');
}
/**
* Escape double quotes
* @param {String} str
* @returns
*/
function escapeQuotes(str) {
return String(str).replaceAll('"', '\\"');
}
/**
* Wrap the value within double quote if needed
* @param {*} value
* @returns
*/
function formatValue(value) {
const hasWhiteSpaces = /\s/g.test(value);
return hasWhiteSpaces ? `"${escapeQuotes(value)}"` : value;
}
/**
* Simplify the Mocha test object to get the information we need want to report
* @param {Mocha.Test} test
* @returns {Object}
*/
function cleanTest(test) {
const err = test.err instanceof Error ? test.err.toString() : false;
const testLocation = getTestLocation(test);
// Remove the test title
const suite = testLocation.slice(0, testLocation.length - 1).join(' > ');
return {
currentRetry: test.currentRetry(),
duration: test.duration,
speed: test.speed,
file: getTestFile(test),
suite,
title: test.title,
err,
};
}
/**
* Get the test path in the suite herarchy
* @example
* ['Root Suite in a file', 'Nested suite', 'test title']
*
* @param {Mocha.Test} test
* @returns {Array<String>}
*/
function getTestLocation(test) {
let path = test.title ? [test.title] : [];
if (test.parent) {
path = getTestLocation(test.parent).concat(path);
}
return path;
}
/**
* Get the relative path to the executed spec file
* @param {Mocha.Test} test
* @returns {String | null}
*/
function getTestFile(test) {
if (test?.file) {
return test?.file;
}
if (test?.parent) {
return getTestFile(test.parent);
}
return null;
}
module.exports = LogReporter;