Have a test to verify the stats

master
Frédéric Matte 2022-12-06 20:03:40 -05:00
parent 8b1a637b3c
commit 0b7776957d
2 changed files with 25 additions and 4 deletions

View File

@ -3,6 +3,8 @@ const metaltest = (title) => {
const only = [] const only = []
const before = [] const before = []
const after = [] const after = []
const end = []
let success = 0 let success = 0
let fail = 0 let fail = 0
@ -16,6 +18,7 @@ const metaltest = (title) => {
runner.only = (name, fn) => { only.push({ name, fn }) } runner.only = (name, fn) => { only.push({ name, fn }) }
runner.before = (fn) => { before.push(fn) } runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) } runner.after = (fn) => { after.push(fn) }
runner.end = (name, fn) => { end.push({ name, fn }) }
const notify = async (reporters, event, ...args) => { const notify = async (reporters, event, ...args) => {
for (const reporter of reporters) { for (const reporter of reporters) {
@ -45,9 +48,24 @@ const metaltest = (title) => {
} }
} }
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
for (const test of end) {
try {
await test.fn(stats)
success++
testSuccess.push(test)
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() for (const fn of after) await fn()
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
notify(reporters, 'end', stats) notify(reporters, 'end', stats)
return stats return stats
} }

View File

@ -13,11 +13,14 @@ test('throw', () => {
}, { name: 'Error', message: 'message' }) }, { name: 'Error', message: 'message' })
}) })
import { runifmain, summaryreporter } from '../index.js' test.end('verify', async (stats) => {
await runifmain(import.meta, async () => {
const stats = await test.run(summaryreporter())
assert.equal(stats.success, 4) assert.equal(stats.success, 4)
assert.equal(stats.fail, 0) assert.equal(stats.fail, 0)
assert.equal(stats.total, 4) assert.equal(stats.total, 4)
assert.equal(stats.title, 'Metaltest') assert.equal(stats.title, 'Metaltest')
assert.equal(stats.testSuccess.length, 4)
assert.equal(stats.testFail.length, 0)
}) })
import { runifmain, summaryreporter, errorreporter } from '../index.js'
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))