metaltest/index.js

72 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2022-11-08 22:29:58 -05:00
import { consolereporter } from './consolereporter.js'
2022-12-04 16:07:28 -05:00
import { linereporter } from './linereporter.js'
2022-11-08 22:29:58 -05:00
import { totalreporter } from './totalreporter.js'
import { runifmain } from './runifmain.js'
2022-12-04 16:30:05 -05:00
import { suite } from './suite.js'
2022-11-08 22:29:58 -05:00
const noreporter = {
start: (title) => { },
before: () => { },
success: (test) => { },
fail: (test, e) => { },
after: () => { },
end: (stats) => { },
}
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 }) }
2022-11-08 22:29:58 -05:00
runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) }
runner.run = async (...reporters) => {
const rs = reporters.map(r => Object.assign({}, noreporter, r))
rs.forEach(r => r.start(title))
rs.forEach(r => r.before())
const tests = only.length ? only : suite
for (const test of tests) {
try {
for (const fn of before) await fn()
await test.fn()
success++
rs.forEach(r => r.success(test))
}
catch (e) {
fail++
testFail.push(Object.assign({}, test, { error: e }))
rs.forEach(r => r.fail(test, e))
}
}
for (const fn of after) await fn()
rs.forEach(r => r.after())
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
rs.forEach(r => r.end(stats))
return stats
}
return runner
}
2022-12-04 16:30:05 -05:00
export { metaltest, consolereporter, linereporter, totalreporter, runifmain, suite }
2022-11-08 22:29:58 -05:00
export default metaltest