From f59a4923a003e7e506a54da94cfeb11b7490a9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Mon, 5 Dec 2022 22:38:52 -0500 Subject: [PATCH] Introduce notify reporter --- lib/metaltest.js | 27 +++++++++++++++------------ reporter/nullreporter.js | 10 ---------- 2 files changed, 15 insertions(+), 22 deletions(-) delete mode 100644 reporter/nullreporter.js diff --git a/lib/metaltest.js b/lib/metaltest.js index 2fdecdd..c4f0192 100644 --- a/lib/metaltest.js +++ b/lib/metaltest.js @@ -1,5 +1,3 @@ -import { nullreporter } from '../reporter/nullreporter.js' - const metaltest = (title) => { const suite = [] const only = [] @@ -19,11 +17,17 @@ const metaltest = (title) => { runner.before = (fn) => { before.push(fn) } runner.after = (fn) => { after.push(fn) } - runner.run = async (...reporters) => { - const rs = reporters.map(r => Object.assign({}, nullreporter, r)) + const notify = async (reporters, event, ...args) => { + for (const reporter of reporters) { + if (!reporter[event]) continue - rs.forEach(r => r.start(title)) - rs.forEach(r => r.before()) + await reporter[event](...args) + } + } + + runner.run = async (...reporters) => { + notify(reporters, 'start', title) + notify(reporters, 'before') const tests = only.length ? only : suite for (const test of tests) { @@ -31,22 +35,21 @@ const metaltest = (title) => { for (const fn of before) await fn() await test.fn() success++ - rs.forEach(r => r.success(test)) + notify(reporters, 'success', test) } catch (e) { fail++ + testFail.push(Object.assign({}, test, { error: e })) - rs.forEach(r => r.fail(test, e)) + notify(reporters, 'fail', test, e) } } for (const fn of after) await fn() - - rs.forEach(r => r.after()) + notify(reporters, 'after') const stats = { title, success, fail, total: success + fail, testSuccess, testFail } - - rs.forEach(r => r.end(stats)) + notify(reporters, 'end', stats) return stats } diff --git a/reporter/nullreporter.js b/reporter/nullreporter.js deleted file mode 100644 index f47bc90..0000000 --- a/reporter/nullreporter.js +++ /dev/null @@ -1,10 +0,0 @@ -const nullreporter = { - start: (title) => { }, - before: () => { }, - success: (test) => { }, - fail: (test, e) => { }, - after: () => { }, - end: (stats) => { }, -} - -export { nullreporter } \ No newline at end of file