|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Mocha = require('mocha'); |
| 4 | +const { |
| 5 | + EVENT_RUN_BEGIN, |
| 6 | + EVENT_RUN_END, |
| 7 | + EVENT_TEST_BEGIN, |
| 8 | + EVENT_TEST_END, |
| 9 | + EVENT_TEST_FAIL, |
| 10 | + EVENT_TEST_PASS, |
| 11 | + EVENT_SUITE_BEGIN, |
| 12 | + EVENT_SUITE_END |
| 13 | +} = Mocha.Runner.constants; |
| 14 | + |
| 15 | + |
| 16 | +// See: https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color |
| 17 | +let disableColor = false; //!(process.stdout.isTTY); |
| 18 | +process.argv.forEach((arg) => { |
| 19 | + if (arg === "--no-color") { disableColor = true; } |
| 20 | +}); |
| 21 | + |
| 22 | +const Colors = { |
| 23 | + "blue": "\x1b[0;34m", |
| 24 | + "blue+": "\x1b[0;1;34m", |
| 25 | + "cyan": "\x1b[0;36m", |
| 26 | + "cyan+": "\x1b[0;1;36m", |
| 27 | + "green": "\x1b[0;32m", |
| 28 | + "green+": "\x1b[0;1;32m", |
| 29 | + "magenta-": "\x1b[0;2;35m", |
| 30 | + "magenta": "\x1b[0;35m", |
| 31 | + "magenta+": "\x1b[0;1;35m", |
| 32 | + "red": "\x1b[0;31m", |
| 33 | + "red+": "\x1b[0;1;31m", |
| 34 | + "yellow": "\x1b[0;33m", |
| 35 | + "yellow+": "\x1b[0;1;33m", |
| 36 | + "dim": "\x1b[0;2;37m", |
| 37 | + "bold": "\x1b[0;1;37m", |
| 38 | + "normal": "\x1b[0m" |
| 39 | +}; |
| 40 | + |
| 41 | +function colorify(text) { |
| 42 | + return unescapeColor(text.replace(/(<([a-z+]+)>)/g, (all, _, color) => { |
| 43 | + if (disableColor) { return ""; } |
| 44 | + |
| 45 | + const seq = Colors[color]; |
| 46 | + if (seq == null) { |
| 47 | + console.log("UNKNOWN COLOR:", color); |
| 48 | + return ""; |
| 49 | + } |
| 50 | + return seq; |
| 51 | + })) + Colors.normal; |
| 52 | +} |
| 53 | + |
| 54 | +function escapeColor(text) { |
| 55 | + return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| 56 | +} |
| 57 | + |
| 58 | +function unescapeColor(text) { |
| 59 | + return text.replace(/>/g, ">").replace(/</g, "<").replace(/&/g, "&"); |
| 60 | +} |
| 61 | + |
| 62 | +function getString(value) { |
| 63 | + if (value instanceof Error) { |
| 64 | + return value.stack; |
| 65 | + } |
| 66 | + return String(value); |
| 67 | +} |
| 68 | + |
| 69 | +// To prevent environments from thinking we're dead due to lack of |
| 70 | +// output, we force output after 20s |
| 71 | +function getTime() { return (new Date()).getTime(); } |
| 72 | +const KEEP_ALIVE = 20 * 1000; |
| 73 | + |
| 74 | +// this reporter outputs test results, indenting two spaces per suite |
| 75 | +class MyReporter { |
| 76 | + constructor(runner) { |
| 77 | + this._errors = [ ]; |
| 78 | + this._indents = 1; |
| 79 | + this._lastLog = getTime(); |
| 80 | + this._lastPass = ""; |
| 81 | + this._lastPrefix = null; |
| 82 | + this._lastPrefixHeader = null; |
| 83 | + this._testLogs = [ ]; |
| 84 | + this._suiteLogs = [ ]; |
| 85 | + this._prefixCount = 0; |
| 86 | + const stats = runner.stats; |
| 87 | + |
| 88 | + runner.once(EVENT_RUN_BEGIN, () => { |
| 89 | + |
| 90 | + }).on(EVENT_SUITE_BEGIN, (suite) => { |
| 91 | + this._suiteLogs.push([ ]); |
| 92 | + suite._ethersLog = (text) => { |
| 93 | + this._suiteLogs[this._suiteLogs.length - 1].push(getString(text)) |
| 94 | + }; |
| 95 | + if (suite.title.trim()) { |
| 96 | + this.log(`<blue+>Suite: ${ escapeColor(suite.title) }`) |
| 97 | + } |
| 98 | + this.increaseIndent(); |
| 99 | + |
| 100 | + }).on(EVENT_SUITE_END, (suite) => { |
| 101 | + this.flush(true); |
| 102 | + this.decreaseIndent(); |
| 103 | + const logs = this._suiteLogs.pop(); |
| 104 | + if (logs.length) { |
| 105 | + logs.join("\n").split("\n").forEach((line) => { |
| 106 | + this.log(` <magenta+>>> <dim>${ escapeColor(line) }`); |
| 107 | + }); |
| 108 | + } |
| 109 | + if (suite.title.trim()) { this.log(""); } |
| 110 | + |
| 111 | + }).on(EVENT_TEST_BEGIN, (test) => { |
| 112 | + this._testLogs.push([ ]); |
| 113 | + test._ethersLog = (text) => { |
| 114 | + this._testLogs[this._testLogs.length - 1].push(getString(text)) |
| 115 | + }; |
| 116 | + |
| 117 | + }).on(EVENT_TEST_END, (test) => { |
| 118 | + const logs = this._testLogs.pop(); |
| 119 | + if (logs.length) { |
| 120 | + this.flush(false); |
| 121 | + logs.join("\n").split("\n").forEach((line) => { |
| 122 | + this.log(` <cyan+>>> <cyan->${ escapeColor(line) }`); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + }).on(EVENT_TEST_PASS, (test) => { |
| 127 | + this.addPass(test.title); |
| 128 | + |
| 129 | + }).on(EVENT_TEST_FAIL, (test, error) => { |
| 130 | + this.flush(); |
| 131 | + this._errors.push({ test, error }); |
| 132 | + this.log( |
| 133 | + ` [ <red+>fail(${ this._errors.length }): <red>${ escapeColor(test.title) } - <normal>${ escapeColor(error.message) } ]` |
| 134 | + ); |
| 135 | + |
| 136 | + }).once(EVENT_RUN_END, () => { |
| 137 | + this.flush(true); |
| 138 | + this.indent = 0; |
| 139 | + |
| 140 | + if (this._errors.length) { |
| 141 | + this._errors.forEach(({ test, error }, index) => { |
| 142 | + this.log("<cyan+>---------------------"); |
| 143 | + this.log(`<red+>ERROR ${ index + 1 }: <red>${ escapeColor(test.title) }`); |
| 144 | + this.log(escapeColor(error.toString())); |
| 145 | + }); |
| 146 | + this.log("<cyan+>====================="); |
| 147 | + } |
| 148 | + |
| 149 | + const { duration, passes, failures } = stats; |
| 150 | + const total = passes + failures; |
| 151 | + this.log(`<bold>Done: <green+>${ passes }<green>/${ total } passed <red>(<red+>${ failures } <red>failed)`); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + log(line) { |
| 156 | + this._lastLog = getTime(); |
| 157 | + const indent = Array(this._indents).join(' '); |
| 158 | + console.log(`${ indent }${ colorify(line) }`); |
| 159 | + } |
| 160 | + |
| 161 | + addPass(line) { |
| 162 | + const prefix = line.split(":")[0]; |
| 163 | + if (prefix === this._lastPrefix) { |
| 164 | + this._prefixCount++; |
| 165 | + if (getTime() - this._lastLog > KEEP_ALIVE) { |
| 166 | + const didLog = this.flush(false); |
| 167 | + // Nothing was output, so show *something* so the |
| 168 | + // environment knows we're still alive and kicking |
| 169 | + if (!didLog) { |
| 170 | + this.log(" <yellow>[ keep-alive; forced output ]") |
| 171 | + } |
| 172 | + } |
| 173 | + } else { |
| 174 | + this.flush(true); |
| 175 | + this._lastPrefixHeader = null; |
| 176 | + this._lastPrefix = prefix; |
| 177 | + this._prefixCount = 1; |
| 178 | + } |
| 179 | + this._lastLine = line; |
| 180 | + } |
| 181 | + |
| 182 | + flush(reset) { |
| 183 | + let didLog = false; |
| 184 | + if (this._lastPrefix != null) { |
| 185 | + if (this._prefixCount === 1 && this._lastPrefixHeader == null) { |
| 186 | + this.log(escapeColor(this._lastLine)); |
| 187 | + didLog = true; |
| 188 | + } else if (this._prefixCount > 0) { |
| 189 | + if (this._lastPrefixHeader !== this._lastPrefix) { |
| 190 | + this.log(`<cyan>${ escapeColor(this._lastPrefix) }:`); |
| 191 | + this._lastPrefixHeader = this._lastPrefix; |
| 192 | + } |
| 193 | + this.log(` - ${ this._prefixCount } tests passed (prefix coalesced)`); |
| 194 | + didLog = true; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (reset) { |
| 199 | + this._lastPrefixHeader = null; |
| 200 | + this._lastPrefix = null; |
| 201 | + } |
| 202 | + |
| 203 | + this._prefixCount = 0; |
| 204 | + |
| 205 | + return didLog; |
| 206 | + } |
| 207 | + |
| 208 | + increaseIndent() { this._indents++; } |
| 209 | + |
| 210 | + decreaseIndent() { this._indents--; } |
| 211 | +} |
| 212 | + |
| 213 | +module.exports = MyReporter; |
0 commit comments