From 48bac473fcf5159cd8f0c288e56a2b0aa82886f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Tue, 6 Dec 2022 20:18:42 -0500 Subject: [PATCH] Extract stats counting --- lib/metaltest.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/metaltest.js b/lib/metaltest.js index 4a3104e..8e5aa64 100644 --- a/lib/metaltest.js +++ b/lib/metaltest.js @@ -5,13 +5,23 @@ const metaltest = (title) => { const after = [] const end = [] - let success = 0 let fail = 0 const testSuccess = [] const testFail = [] - const stats = () => { + const stats = (type, test, error) => { + switch (type) { + case 'success': + success++ + testSuccess.push(test) + break + case 'fail': + fail++ + testFail.push(Object.assign({}, test, { error })) + break + } + return { title, success, fail, total: success + fail, testSuccess, testFail } } @@ -40,14 +50,11 @@ const metaltest = (title) => { try { for (const fn of before) await fn() await test.fn() - - success++ - testSuccess.push(test) + stats('success', test) notify(reporters, 'success', test) } catch (error) { - fail++ - testFail.push(Object.assign({}, test, { error })) + stats('fail', test, error) notify(reporters, 'fail', test, error) } } @@ -56,20 +63,19 @@ const metaltest = (title) => { try { await test.fn(stats()) - success++ - testSuccess.push(test) + stats('success', test) notify(reporters, 'success', test) } catch (error) { - fail++ - testFail.push(Object.assign({}, test, { error })) + stats('fail', test, error) notify(reporters, 'fail', test, error) } } for (const fn of after) await fn() - notify(reporters, 'end', stats()) - return stats + const r = stats() + notify(reporters, 'end', r) + return r } return runner