|
| 1 | +const Mocha = require('mocha') |
| 2 | + |
| 3 | +class Reporter { |
| 4 | + constructor (runner) { |
| 5 | + this.failedTests = [] |
| 6 | + |
| 7 | + runner.on(Mocha.Runner.constants.EVENT_RUN_BEGIN, () => { |
| 8 | + console.log('Starting tests') |
| 9 | + }) |
| 10 | + |
| 11 | + runner.on(Mocha.Runner.constants.EVENT_RUN_END, () => { |
| 12 | + console.log('Tests finished') |
| 13 | + console.log() |
| 14 | + console.log('****************') |
| 15 | + console.log('* TESTS REPORT *') |
| 16 | + console.log('****************') |
| 17 | + console.log() |
| 18 | + console.log(`Executed ${runner.stats.suites} suites with ${runner.stats.tests} tests in ${runner.stats.duration} ms`) |
| 19 | + console.log(` Passed: ${runner.stats.passes}`) |
| 20 | + console.log(` Skipped: ${runner.stats.pending}`) |
| 21 | + console.log(` Failed: ${runner.stats.failures}`) |
| 22 | + if (this.failedTests.length > 0) { |
| 23 | + console.log() |
| 24 | + console.log(' Failed test details') |
| 25 | + this.failedTests.forEach((failedTest, index) => { |
| 26 | + console.log() |
| 27 | + console.log(` ${index + 1}.'${failedTest.test.fullTitle()}'`) |
| 28 | + console.log(` Name: ${failedTest.error.name}`) |
| 29 | + console.log(` Message: ${failedTest.error.message}`) |
| 30 | + console.log(` Code: ${failedTest.error.code}`) |
| 31 | + console.log(` Stack: ${failedTest.error.stack}`) |
| 32 | + }) |
| 33 | + } |
| 34 | + console.log() |
| 35 | + }) |
| 36 | + |
| 37 | + runner.on(Mocha.Runner.constants.EVENT_SUITE_BEGIN, (suite) => { |
| 38 | + if (suite.root) { |
| 39 | + return |
| 40 | + } |
| 41 | + console.log(`Starting suite '${suite.title}'`) |
| 42 | + }) |
| 43 | + |
| 44 | + runner.on(Mocha.Runner.constants.EVENT_SUITE_END, (suite) => { |
| 45 | + if (suite.root) { |
| 46 | + return |
| 47 | + } |
| 48 | + console.log(`Suite '${suite.title}' finished`) |
| 49 | + console.log() |
| 50 | + }) |
| 51 | + |
| 52 | + runner.on(Mocha.Runner.constants.EVENT_TEST_BEGIN, (test) => { |
| 53 | + console.log(`Starting test '${test.title}'`) |
| 54 | + }) |
| 55 | + |
| 56 | + runner.on(Mocha.Runner.constants.EVENT_TEST_PASS, (test) => { |
| 57 | + console.log(`Test '${test.title}' passed in ${test.duration} ms`) |
| 58 | + }) |
| 59 | + |
| 60 | + runner.on(Mocha.Runner.constants.EVENT_TEST_PENDING, (test) => { |
| 61 | + console.log(`Test '${test.title}' skipped in ${test.duration} ms`) |
| 62 | + }) |
| 63 | + |
| 64 | + runner.on(Mocha.Runner.constants.EVENT_TEST_FAIL, (test, error) => { |
| 65 | + this.failedTests.push({ test, error }) |
| 66 | + console.log(`Test '${test.title}' failed in ${test.duration} ms with ${error}`) |
| 67 | + }) |
| 68 | + |
| 69 | + runner.on(Mocha.Runner.constants.EVENT_TEST_END, (test) => { |
| 70 | + console.log() |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = Reporter |
0 commit comments