|
| 1 | +import { signal } from '../constants'; |
| 2 | + |
| 3 | +exports = module.exports = reporter; |
| 4 | +function reporter(runner) { |
| 5 | + let result = { |
| 6 | + passes: [], |
| 7 | + failures: [], |
| 8 | + pass: true, |
| 9 | + }; |
| 10 | + |
| 11 | + runner.on('pass', (test) => { |
| 12 | + const {index} = getIndexAndTitle(test.fullTitle()); |
| 13 | + // add pass |
| 14 | + result.passes.push({ |
| 15 | + msg: `Task ${index} Complete`, |
| 16 | + taskPosition: index, |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + runner.on('fail', (test, err: Error) => { |
| 21 | + const {msg, index} = getIndexAndTitle(test.fullTitle()); |
| 22 | + // add fail |
| 23 | + result.failures.push({ |
| 24 | + msg, |
| 25 | + taskPosition: index - 1, |
| 26 | + // body: test.body, |
| 27 | + timedOut: test.timedOut, |
| 28 | + // duration: test.duration |
| 29 | + }); |
| 30 | + result.pass = false; |
| 31 | + }); |
| 32 | + |
| 33 | + runner.on('end', function() { |
| 34 | + // anything before the signal will be captured as log |
| 35 | + process.stdout.write(signal + JSON.stringify(result, null, 2)); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +interface IndexTitle { |
| 40 | + index: number; |
| 41 | + msg: string; |
| 42 | +} |
| 43 | + |
| 44 | +function getIndexAndTitle(title: string): IndexTitle { |
| 45 | + // tests prefixed with task number: "01 title" |
| 46 | + const indexString = title.match(/^[0-9]+/); |
| 47 | + if (!indexString) { |
| 48 | + throw 'Tests should begin with a number, indicating the task number'; |
| 49 | + } |
| 50 | + return { |
| 51 | + index: parseInt(indexString[0], 10), |
| 52 | + msg: title.slice(indexString[0].length + 1), |
| 53 | + }; |
| 54 | +} |
0 commit comments