|
| 1 | +import { reporters, Runner, Suite, MochaOptions } from "mocha"; |
| 2 | + |
| 3 | +const EVENT_SUITE_BEGIN = Runner.constants.EVENT_SUITE_BEGIN; |
| 4 | +const EVENT_SUITE_END = Runner.constants.EVENT_SUITE_END; |
| 5 | +const EVENT_TEST_BEGIN = Runner.constants.EVENT_TEST_BEGIN; |
| 6 | +const EVENT_TEST_END = Runner.constants.EVENT_TEST_END; |
| 7 | +const EVENT_TEST_PASS = Runner.constants.EVENT_TEST_PASS; |
| 8 | +const EVENT_TEST_FAIL = Runner.constants.EVENT_TEST_FAIL; |
| 9 | +const EVENT_TEST_PENDING = Runner.constants.EVENT_TEST_PENDING; |
| 10 | + |
| 11 | +class CodewarsReporter extends reporters.Base { |
| 12 | + // To allow default import |
| 13 | + static default = CodewarsReporter; |
| 14 | + constructor(runner: Runner, options?: MochaOptions) { |
| 15 | + super(runner, options); |
| 16 | + |
| 17 | + runner.on(EVENT_SUITE_BEGIN, (suite) => { |
| 18 | + if (suite.title) groupStart(suite.title); |
| 19 | + }); |
| 20 | + |
| 21 | + runner.on(EVENT_SUITE_END, (suite) => { |
| 22 | + if (suite.title) completedIn(getSuiteDuration(suite)); |
| 23 | + }); |
| 24 | + |
| 25 | + runner.on(EVENT_TEST_BEGIN, (test) => { |
| 26 | + testStart(test.title); |
| 27 | + }); |
| 28 | + |
| 29 | + runner.on(EVENT_TEST_END, (test) => { |
| 30 | + completedIn(test.duration); |
| 31 | + }); |
| 32 | + |
| 33 | + runner.on(EVENT_TEST_PASS, (_) => { |
| 34 | + passed(); |
| 35 | + }); |
| 36 | + |
| 37 | + runner.on(EVENT_TEST_PENDING, (_) => { |
| 38 | + skipped(); |
| 39 | + }); |
| 40 | + |
| 41 | + runner.on(EVENT_TEST_FAIL, (test, err) => { |
| 42 | + if (test.timedOut) return failed("Timed Out"); |
| 43 | + |
| 44 | + // From fast-check |
| 45 | + // TODO Improve the output by providing custom reporter. |
| 46 | + // https://github.com/dubzzz/fast-check/blob/master/documentation/1-Guides/Tips.md#customize-the-reported-error |
| 47 | + if ( |
| 48 | + err instanceof Error && |
| 49 | + /^Property failed after \d+ tests/.test(err.message) |
| 50 | + ) { |
| 51 | + failed(err.message); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (err instanceof Error && !/^AssertionError/.test(err.name)) { |
| 56 | + errored(err.stack || err.toString()); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + failed(err.message); |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +const getSuiteDuration = ({ tests, suites }: Suite): number => |
| 66 | + tests.reduce((a, t) => a + (t.duration || 0), 0) + |
| 67 | + suites.reduce((a, s) => a + getSuiteDuration(s), 0); |
| 68 | + |
| 69 | +const escapeLF = (text: string) => text.replace(/\n/g, "<:LF:>"); |
| 70 | + |
| 71 | +const groupStart = (s: string) => console.log(`\n<DESCRIBE::>${escapeLF(s)}`); |
| 72 | +const testStart = (s: string) => console.log(`\n<IT::>${escapeLF(s)}`); |
| 73 | +const failed = (s: string) => console.log(`\n<FAILED::>${escapeLF(s)}`); |
| 74 | +const errored = (s: string) => console.log(`\n<ERROR::>${escapeLF(s)}`); |
| 75 | +const passed = () => console.log("\n<PASSED::>Test Passed"); |
| 76 | +const skipped = () => console.log("\n<LOG::>Test Skipped"); |
| 77 | +const completedIn = (d?: number) => |
| 78 | + console.log(`\n<COMPLETEDIN::>${typeof d === "undefined" ? "" : d}`); |
| 79 | + |
| 80 | +export = CodewarsReporter; |
0 commit comments