Skip to content

Commit 9e5d65a

Browse files
write custom test reporter for more verbose output
Implemented a simple custom mocha test reporter to replace the default one. Made test report more developer friendly.
1 parent 94d6a40 commit 9e5d65a

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
},
4747
"scripts": {
4848
"lint": "standard */*.js test/**/*.js",
49-
"test": "npm run lint && mocha test/test-download.js test/test-*"
49+
"test": "npm run lint && mocha --reporter=test/reporter.js test/test-download.js test/test-*"
5050
}
5151
}

test/reporter.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)