diff --git a/lib/metaltest.js b/lib/metaltest.js index c0fd209..7881c16 100644 --- a/lib/metaltest.js +++ b/lib/metaltest.js @@ -3,6 +3,8 @@ const metaltest = (title) => { const only = [] const before = [] const after = [] + const end = [] + let success = 0 let fail = 0 @@ -16,6 +18,7 @@ const metaltest = (title) => { runner.only = (name, fn) => { only.push({ name, fn }) } runner.before = (fn) => { before.push(fn) } runner.after = (fn) => { after.push(fn) } + runner.end = (name, fn) => { end.push({ name, fn }) } const notify = async (reporters, event, ...args) => { 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() - const stats = { title, success, fail, total: success + fail, testSuccess, testFail } notify(reporters, 'end', stats) return stats } diff --git a/lib/metaltest.test.js b/lib/metaltest.test.js index c45a7b6..33b7529 100644 --- a/lib/metaltest.test.js +++ b/lib/metaltest.test.js @@ -13,11 +13,14 @@ test('throw', () => { }, { name: 'Error', message: 'message' }) }) -import { runifmain, summaryreporter } from '../index.js' -await runifmain(import.meta, async () => { - const stats = await test.run(summaryreporter()) +test.end('verify', async (stats) => { assert.equal(stats.success, 4) assert.equal(stats.fail, 0) assert.equal(stats.total, 4) 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()))