|
| 1 | +import Anser from "anser"; |
| 2 | + |
| 3 | +// Get types from @jest packages. |
| 4 | +// These can be `devDependencies` because no JS is emitted and we're currently |
| 5 | +// not emitting declarations. |
| 6 | +import { ConsoleBuffer } from "@jest/console"; |
| 7 | +import { |
| 8 | + Test, |
| 9 | + Reporter, |
| 10 | + Config, |
| 11 | + Context, |
| 12 | + ReporterOnStartOptions, |
| 13 | +} from "@jest/reporters"; |
| 14 | +import { |
| 15 | + AssertionResult, |
| 16 | + TestResult, |
| 17 | + AggregatedResult, |
| 18 | +} from "@jest/test-result"; |
| 19 | + |
| 20 | +export type CodewarsReporterOptions = {}; |
| 21 | + |
| 22 | +export default class CodewarsReporter implements Reporter { |
| 23 | + _globalConfig: Config.GlobalConfig; |
| 24 | + _options: CodewarsReporterOptions; |
| 25 | + _shouldFail: boolean | undefined; |
| 26 | + rootDir: string; |
| 27 | + constructor( |
| 28 | + globalConfig: Config.GlobalConfig, |
| 29 | + options: CodewarsReporterOptions |
| 30 | + ) { |
| 31 | + this._globalConfig = globalConfig; |
| 32 | + this._options = options; |
| 33 | + this.rootDir = globalConfig.rootDir; |
| 34 | + } |
| 35 | + |
| 36 | + onRunStart(results: AggregatedResult, options: ReporterOnStartOptions): void { |
| 37 | + // |
| 38 | + } |
| 39 | + |
| 40 | + onTestStart(test: Test): void { |
| 41 | + // |
| 42 | + } |
| 43 | + |
| 44 | + onTestResult( |
| 45 | + test: Test, |
| 46 | + testResult: TestResult, |
| 47 | + aggregatedResult: AggregatedResult |
| 48 | + ): void { |
| 49 | + const results = testResult.testResults; |
| 50 | + _logSuite(groupTestsBySuites(results)); |
| 51 | + _showConsoleOutput(testResult.console, this.rootDir); |
| 52 | + // Show failure message showing more details like source. |
| 53 | + // Maybe this needs to be configurable. |
| 54 | + if (!testResult.failureMessage) return; |
| 55 | + |
| 56 | + const failureMsg = testResult.failureMessage.trim(); |
| 57 | + if (!failureMsg) return; |
| 58 | + |
| 59 | + const msg = escapeLF(ansi2html(failureMsg)); |
| 60 | + console.log( |
| 61 | + `\n<LOG:HTML:Failures><pre class="ansi"><code>${msg}</code></pre>` |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + onRunComplete(contexts: Set<Context>, results: AggregatedResult): void { |
| 66 | + // |
| 67 | + } |
| 68 | + |
| 69 | + getLastError() { |
| 70 | + if (this._shouldFail) { |
| 71 | + // Force exit code non zero |
| 72 | + return new Error("Test Failed"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// https://github.com/sindresorhus/extract-stack |
| 78 | +function extractStack(stack: string) { |
| 79 | + if (!stack) return ""; |
| 80 | + const match = stack.match(/(?:\n {4}at .*)+/); |
| 81 | + if (!match) return ""; |
| 82 | + return match[0].slice(1); |
| 83 | +} |
| 84 | + |
| 85 | +const escapeLF = (s: string) => s.replace(/\n/g, "<:LF:>"); |
| 86 | +const escapeHtml = (s: string) => |
| 87 | + s.replace(/[&<>]/g, (c) => (c == "&" ? "&" : c == "<" ? "<" : ">")); |
| 88 | +const ansi2html = (s: string) => |
| 89 | + Anser.ansiToHtml(escapeHtml(s), { use_classes: true }); |
| 90 | + |
| 91 | +type TestSuiteGroup = { |
| 92 | + suites: TestSuiteGroup[]; |
| 93 | + tests: AssertionResult[]; |
| 94 | + title: string; |
| 95 | +}; |
| 96 | + |
| 97 | +// https://github.com/facebook/jest/blob/179b3e8cd9838789254e2a2d63b32a699ca236d3/packages/jest-cli/src/reporters/verbose_reporter.js#L35 |
| 98 | +// Note that if suites with same names exist, they're merged. |
| 99 | +function groupTestsBySuites(testResults: AssertionResult[]) { |
| 100 | + const root: TestSuiteGroup = { suites: [], tests: [], title: "" }; |
| 101 | + for (const testResult of testResults) { |
| 102 | + let targetSuite = root; |
| 103 | + // Find the target suite for this test, creating nested suites as necessary. |
| 104 | + for (const title of testResult.ancestorTitles) { |
| 105 | + let matchingSuite = targetSuite.suites.find((s) => s.title === title); |
| 106 | + if (!matchingSuite) { |
| 107 | + matchingSuite = { suites: [], tests: [], title }; |
| 108 | + targetSuite.suites.push(matchingSuite); |
| 109 | + } |
| 110 | + targetSuite = matchingSuite; |
| 111 | + } |
| 112 | + targetSuite.tests.push(testResult); |
| 113 | + } |
| 114 | + return root; |
| 115 | +} |
| 116 | + |
| 117 | +function _logSuite(suite: TestSuiteGroup) { |
| 118 | + const hasTitle = suite.title !== ""; |
| 119 | + if (hasTitle) console.log(`\n<DESCRIBE::>${suite.title}`); |
| 120 | + for (const t of suite.tests) _logTest(t); |
| 121 | + for (const s of suite.suites) _logSuite(s); |
| 122 | + if (hasTitle) console.log(`\n<COMPLETEDIN::>`); |
| 123 | +} |
| 124 | + |
| 125 | +function _logTest(test: AssertionResult) { |
| 126 | + console.log(`\n<IT::>${test.title}`); |
| 127 | + switch (test.status) { |
| 128 | + case "passed": |
| 129 | + console.log(`\n<PASSED::>Test Passed`); |
| 130 | + break; |
| 131 | + case "failed": { |
| 132 | + console.log(`\n<FAILED::>Test Failed`); |
| 133 | + // We need to separate this because `<FAILED::>` doesn't support HTML mode at the moment. |
| 134 | + const msg = escapeLF( |
| 135 | + ansi2html(_collectFailureMessages(test.failureMessages).trim()) |
| 136 | + ); |
| 137 | + console.log( |
| 138 | + `\n<LOG:HTML:Failure><pre class="ansi"><code>${msg}</code></pre>` |
| 139 | + ); |
| 140 | + break; |
| 141 | + } |
| 142 | + case "pending": |
| 143 | + console.log(`\n<LOG::>Test Pending`); |
| 144 | + break; |
| 145 | + case "todo": |
| 146 | + console.log(`\n<LOG::>Test TODO`); |
| 147 | + break; |
| 148 | + } |
| 149 | + const time = test.duration ? test.duration.toFixed(0) : ""; |
| 150 | + console.log(`\n<COMPLETEDIN::>${time}`); |
| 151 | +} |
| 152 | + |
| 153 | +function _collectFailureMessages(messages: string[]) { |
| 154 | + const lines: string[] = []; |
| 155 | + for (const m of messages) { |
| 156 | + lines.push(m.replace(extractStack(m), "")); |
| 157 | + } |
| 158 | + return lines.join("\n\n"); |
| 159 | +} |
| 160 | + |
| 161 | +function _showConsoleOutput(cs: ConsoleBuffer | undefined, rootDir: string) { |
| 162 | + if (!cs) return; |
| 163 | + for (const c of cs) { |
| 164 | + const message = c.message; |
| 165 | + if (c.type === "error") { |
| 166 | + console.error(message); |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + // Don't prepend location information when output command is used |
| 171 | + if (/^<(?:LOG|TAB|PROP|OUT|SWAP|TAG):(?:[A-Z]*):(?:[^>]*)>/.test(message)) { |
| 172 | + console.log(message); |
| 173 | + } else { |
| 174 | + const from = c.origin.replace(rootDir, "").replace(/^\//, ""); |
| 175 | + console.log(`\n<LOG::console.${c.type} ${from}>${escapeLF(message)}`); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments