1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/**
* A SW Reporter created to be compatible with mocha-headless-chrome's repoter
* See: https://github.com/direct-adv-interfaces/mocha-headless-chrome/blob/273d9b8bc7445ea1196b10ad0eaf0a8bce6cbd5f/lib/runner.js#L68
*/
const { Spec } = Mocha.reporters
const {
EVENT_RUN_END,
EVENT_TEST_FAIL,
EVENT_TEST_PASS,
EVENT_TEST_PENDING
} = Mocha.Runner.constants
function SWReporter (runner, options) {
Spec.call(this, runner, options)
const all = []
const passes = []
const failures = []
const pending = []
const error = (err) => {
if (!err) return {}
const res = {}
Object.getOwnPropertyNames(err).forEach((key) => (res[key] = err[key]))
return res
}
const clean = (test) => ({
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
err: error(test.err)
})
const getResult = (stats) => ({
result: {
stats: {
tests: all.length,
passes: passes.length,
pending: pending.length,
failures: failures.length,
start: stats.start.toISOString(),
end: stats.end.toISOString(),
duration: stats.duration
},
tests: all.map(clean),
pending: pending.map(clean),
failures: failures.map(clean),
passes: passes.map(clean)
}
})
runner
.on(EVENT_TEST_PASS, (test) => {
passes.push(test)
all.push(test)
})
.on(EVENT_TEST_FAIL, (test) => {
failures.push(test)
all.push(test)
})
.on(EVENT_TEST_PENDING, (test) => {
pending.push(test)
all.push(test)
})
.once(EVENT_RUN_END, () => {
const result = getResult(runner.stats)
const channel = new BroadcastChannel('sw-result')
channel.postMessage(JSON.stringify(result))
})
}
Mocha.utils.inherits(SWReporter, Spec)
|