forked from ethers-io/ethers.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reporter.cjs
217 lines (185 loc) · 6.62 KB
/
reporter.cjs
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
'use strict';
/* c8 ignore start */
const Mocha = require('mocha');
const {
EVENT_RUN_BEGIN,
EVENT_RUN_END,
EVENT_TEST_BEGIN,
EVENT_TEST_END,
EVENT_TEST_FAIL,
EVENT_TEST_PASS,
EVENT_SUITE_BEGIN,
EVENT_SUITE_END
} = Mocha.Runner.constants;
// See: https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color
let disableColor = false; //!(process.stdout.isTTY);
process.argv.forEach((arg) => {
if (arg === "--no-color") { disableColor = true; }
});
const Colors = {
"blue": "\x1b[0;34m",
"blue+": "\x1b[0;1;34m",
"cyan": "\x1b[0;36m",
"cyan+": "\x1b[0;1;36m",
"green": "\x1b[0;32m",
"green+": "\x1b[0;1;32m",
"magenta-": "\x1b[0;2;35m",
"magenta": "\x1b[0;35m",
"magenta+": "\x1b[0;1;35m",
"red": "\x1b[0;31m",
"red+": "\x1b[0;1;31m",
"yellow": "\x1b[0;33m",
"yellow+": "\x1b[0;1;33m",
"dim": "\x1b[0;2;37m",
"bold": "\x1b[0;1;37m",
"normal": "\x1b[0m"
};
function colorify(text) {
return unescapeColor(text.replace(/(<([a-z+]+)>)/g, (all, _, color) => {
if (disableColor) { return ""; }
const seq = Colors[color];
if (seq == null) {
console.log("UNKNOWN COLOR:", color);
return "";
}
return seq;
})) + (disableColor ? "": Colors.normal);
}
function escapeColor(text) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
function unescapeColor(text) {
return text.replace(/>/g, ">").replace(/</g, "<").replace(/&/g, "&");
}
function getString(value) {
if (value instanceof Error) {
return value.stack;
}
return String(value);
}
// To prevent environments from thinking we're dead due to lack of
// output, we force output after 20s
function getTime() { return (new Date()).getTime(); }
const KEEP_ALIVE = 20 * 1000;
// this reporter outputs test results, indenting two spaces per suite
class MyReporter {
constructor(runner) {
this._errors = [ ];
this._indents = 1;
this._lastLog = getTime();
this._lastPass = "";
this._lastPrefix = null;
this._lastPrefixHeader = null;
this._testLogs = [ ];
this._suiteLogs = [ ];
this._prefixCount = 0;
const stats = runner.stats;
runner.once(EVENT_RUN_BEGIN, () => {
}).on(EVENT_SUITE_BEGIN, (suite) => {
this._suiteLogs.push([ ]);
suite._ethersLog = (text) => {
this._suiteLogs[this._suiteLogs.length - 1].push(getString(text))
};
if (suite.title.trim()) {
this.log(`<blue+>Suite: ${ escapeColor(suite.title) }`)
}
this.increaseIndent();
}).on(EVENT_SUITE_END, (suite) => {
this.flush(true);
this.decreaseIndent();
const logs = this._suiteLogs.pop();
if (logs.length) {
logs.join("\n").split("\n").forEach((line) => {
this.log(` <magenta+>>> <dim>${ escapeColor(line) }`);
});
}
if (suite.title.trim()) { this.log(""); }
}).on(EVENT_TEST_BEGIN, (test) => {
this._testLogs.push([ ]);
test._ethersLog = (text) => {
this._testLogs[this._testLogs.length - 1].push(getString(text))
};
}).on(EVENT_TEST_END, (test) => {
const logs = this._testLogs.pop();
if (logs.length) {
this.flush(false);
logs.join("\n").split("\n").forEach((line) => {
this.log(` <cyan+>>> <cyan->${ escapeColor(line) }`);
});
}
}).on(EVENT_TEST_PASS, (test) => {
this.addPass(test.title);
}).on(EVENT_TEST_FAIL, (test, error) => {
this.flush();
this._errors.push({ test, error });
this.log(
` [ <red+>fail(${ this._errors.length }): <red>${ escapeColor(test.title) } - <normal>${ escapeColor(error.message) } ]`
);
}).once(EVENT_RUN_END, () => {
this.flush(true);
this.indent = 0;
if (this._errors.length) {
this._errors.forEach(({ test, error }, index) => {
this.log("<cyan+>---------------------");
this.log(`<red+>ERROR ${ index + 1 }: <red>${ escapeColor(test.title) }`);
this.log(escapeColor(error.toString()));
});
this.log("<cyan+>=====================");
}
const { duration, passes, failures } = stats;
const total = passes + failures;
this.log(`<bold>Done: <green+>${ passes }<green>/${ total } passed <red>(<red+>${ failures } <red>failed)`);
});
}
log(line) {
this._lastLog = getTime();
const indent = Array(this._indents).join(' ');
console.log(`${ indent }${ colorify(line) }`);
}
addPass(line) {
const prefix = line.split(":")[0];
if (prefix === this._lastPrefix) {
this._prefixCount++;
if (getTime() - this._lastLog > KEEP_ALIVE) {
const didLog = this.flush(false);
// Nothing was output, so show *something* so the
// environment knows we're still alive and kicking
if (!didLog) {
this.log(" <yellow>[ keep-alive; forced output ]")
}
}
} else {
this.flush(true);
this._lastPrefixHeader = null;
this._lastPrefix = prefix;
this._prefixCount = 1;
}
this._lastLine = line;
}
flush(reset) {
let didLog = false;
if (this._lastPrefix != null) {
if (this._prefixCount === 1 && this._lastPrefixHeader == null) {
this.log(escapeColor(this._lastLine));
didLog = true;
} else if (this._prefixCount > 0) {
if (this._lastPrefixHeader !== this._lastPrefix) {
this.log(`<cyan>${ escapeColor(this._lastPrefix) }:`);
this._lastPrefixHeader = this._lastPrefix;
}
this.log(` - ${ this._prefixCount } tests passed (prefix coalesced)`);
didLog = true;
}
}
if (reset) {
this._lastPrefixHeader = null;
this._lastPrefix = null;
}
this._prefixCount = 0;
return didLog;
}
increaseIndent() { this._indents++; }
decreaseIndent() { this._indents--; }
}
module.exports = MyReporter;
/* c8 ignore stop */