57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
const metaltest = (title) => {
|
|
const suite = []
|
|
const only = []
|
|
const before = []
|
|
const after = []
|
|
|
|
let success = 0
|
|
let fail = 0
|
|
const testSuccess = []
|
|
const testFail = []
|
|
|
|
const runner = (name, fn) => {
|
|
suite.push({ name, fn })
|
|
}
|
|
|
|
runner.only = (name, fn) => { only.push({ name, fn }) }
|
|
runner.before = (fn) => { before.push(fn) }
|
|
runner.after = (fn) => { after.push(fn) }
|
|
|
|
const notify = async (reporters, event, ...args) => {
|
|
for (const reporter of reporters) {
|
|
if (!reporter[event]) continue
|
|
|
|
await reporter[event](...args)
|
|
}
|
|
}
|
|
|
|
runner.run = async (...reporters) => {
|
|
notify(reporters, 'start', title)
|
|
|
|
const tests = only.length ? only : suite
|
|
for (const test of tests) {
|
|
try {
|
|
for (const fn of before) await fn()
|
|
await test.fn()
|
|
success++
|
|
notify(reporters, 'success', test)
|
|
}
|
|
catch (error) {
|
|
fail++
|
|
|
|
testFail.push(Object.assign({}, test, { error }))
|
|
notify(reporters, 'fail', test, error)
|
|
}
|
|
}
|
|
|
|
for (const fn of after) await fn()
|
|
|
|
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
|
|
notify(reporters, 'end', stats)
|
|
return stats
|
|
}
|
|
|
|
return runner
|
|
}
|
|
|
|
export { metaltest } |