Introduce notify reporter

master
Frédéric Matte 2022-12-05 22:38:52 -05:00
parent 850240e9ef
commit f59a4923a0
2 changed files with 15 additions and 22 deletions

View File

@ -1,5 +1,3 @@
import { nullreporter } from '../reporter/nullreporter.js'
const metaltest = (title) => {
const suite = []
const only = []
@ -19,11 +17,17 @@ const metaltest = (title) => {
runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) }
runner.run = async (...reporters) => {
const rs = reporters.map(r => Object.assign({}, nullreporter, r))
const notify = async (reporters, event, ...args) => {
for (const reporter of reporters) {
if (!reporter[event]) continue
rs.forEach(r => r.start(title))
rs.forEach(r => r.before())
await reporter[event](...args)
}
}
runner.run = async (...reporters) => {
notify(reporters, 'start', title)
notify(reporters, 'before')
const tests = only.length ? only : suite
for (const test of tests) {
@ -31,22 +35,21 @@ const metaltest = (title) => {
for (const fn of before) await fn()
await test.fn()
success++
rs.forEach(r => r.success(test))
notify(reporters, 'success', test)
}
catch (e) {
fail++
testFail.push(Object.assign({}, test, { error: e }))
rs.forEach(r => r.fail(test, e))
notify(reporters, 'fail', test, e)
}
}
for (const fn of after) await fn()
rs.forEach(r => r.after())
notify(reporters, 'after')
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
rs.forEach(r => r.end(stats))
notify(reporters, 'end', stats)
return stats
}

View File

@ -1,10 +0,0 @@
const nullreporter = {
start: (title) => { },
before: () => { },
success: (test) => { },
fail: (test, e) => { },
after: () => { },
end: (stats) => { },
}
export { nullreporter }