metaltest/lib/metaltest.js

95 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2022-12-05 17:07:06 -05:00
const metaltest = (title) => {
const suite = []
const only = []
2022-12-30 13:42:42 -05:00
const skipped = []
2022-12-05 17:07:06 -05:00
const before = []
const after = []
2022-12-06 20:03:40 -05:00
const end = []
2022-12-05 17:07:06 -05:00
let success = 0
let fail = 0
2022-12-30 13:42:42 -05:00
let skip = 0
2022-12-05 17:07:06 -05:00
const testSuccess = []
const testFail = []
2022-12-06 20:18:42 -05:00
const stats = (type, test, error) => {
switch (type) {
case 'success':
success++
testSuccess.push(test)
break
case 'fail':
fail++
testFail.push(Object.assign({}, test, { error }))
break
}
2022-12-30 13:42:42 -05:00
return { title, success, fail, skip, total: success + fail, testSuccess, testFail }
2022-12-06 20:05:29 -05:00
}
2022-12-05 17:07:06 -05:00
const runner = (name, fn) => {
suite.push({ name, fn })
}
runner.only = (name, fn) => { only.push({ name, fn }) }
2022-12-30 13:42:42 -05:00
runner.skip = (name, fn) => { skipped.push({ name, fn }) }
2022-12-05 17:07:06 -05:00
runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) }
2022-12-06 20:03:40 -05:00
runner.end = (name, fn) => { end.push({ name, fn }) }
2022-12-05 17:07:06 -05:00
2022-12-05 22:38:52 -05:00
const notify = async (reporters, event, ...args) => {
for (const reporter of reporters) {
if (!reporter[event]) continue
2022-12-05 17:07:06 -05:00
2022-12-05 22:38:52 -05:00
await reporter[event](...args)
}
}
runner.run = async (...reporters) => {
2022-12-12 02:30:29 -05:00
await notify(reporters, 'start', title)
2022-12-05 17:07:06 -05:00
const tests = only.length ? only : suite
2022-12-30 13:42:42 -05:00
if (only.length) {
for (const test of suite) await notify(reporters, 'skip', test)
}
for (const test of skipped) await notify(reporters, 'skip', test)
skip = only.length ? suite.length : skip
skip += skipped.length
2022-12-05 17:07:06 -05:00
for (const test of tests) {
try {
for (const fn of before) await fn()
await test.fn()
2022-12-06 20:18:42 -05:00
stats('success', test)
2022-12-05 22:38:52 -05:00
notify(reporters, 'success', test)
2022-12-05 17:07:06 -05:00
}
2022-12-05 22:41:37 -05:00
catch (error) {
2022-12-06 20:18:42 -05:00
stats('fail', test, error)
2022-12-12 02:30:29 -05:00
await notify(reporters, 'fail', test, error)
2022-12-05 17:07:06 -05:00
}
}
2022-12-06 20:03:40 -05:00
for (const test of end) {
try {
2022-12-06 20:05:29 -05:00
await test.fn(stats())
2022-12-06 20:03:40 -05:00
2022-12-06 20:18:42 -05:00
stats('success', test)
2022-12-12 02:30:29 -05:00
await notify(reporters, 'success', test)
2022-12-06 20:03:40 -05:00
} catch (error) {
2022-12-06 20:18:42 -05:00
stats('fail', test, error)
2022-12-12 02:30:29 -05:00
await notify(reporters, 'fail', test, error)
2022-12-06 20:03:40 -05:00
}
}
2022-12-05 17:07:06 -05:00
for (const fn of after) await fn()
2022-12-06 20:18:42 -05:00
const r = stats()
2022-12-12 02:30:29 -05:00
await notify(reporters, 'end', r)
2022-12-06 20:18:42 -05:00
return r
2022-12-05 17:07:06 -05:00
}
return runner
}
export { metaltest }