From: Chris Duncan Date: Sun, 8 Dec 2024 02:58:58 +0000 (-0800) Subject: Reactivate regular tests on testing webpage. Tweak test result messaging. Create... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=19af4ae846ef859d1518d98a11a870617daca6e8;p=libnemo.git Reactivate regular tests on testing webpage. Tweak test result messaging. Create suite function to group tests and implement in tools test. --- diff --git a/GLOBALS.mjs b/GLOBALS.mjs index e2657fb..7e7d4e8 100644 --- a/GLOBALS.mjs +++ b/GLOBALS.mjs @@ -16,84 +16,91 @@ if (globalThis.sessionStorage == null) { }) } -const errors = [] -const logs = [] -function error (...args) { - errors.push(args) - console.error(...args) +const failures = [] +const passes = [] +function fail (...args) { + failures.push(args) + console.error(`%cFAIL `, 'color:red', ...args) } -function log (...args) { - logs.push(args) - console.log(...args) +function pass (...args) { + passes.push(args) + console.log(`%cPASS `, 'color:green', ...args) } + /** * Who watches the watchers? */ -console.assert(errors.length === 0) -console.assert(logs.length === 0) +console.assert(failures.length === 0) +console.assert(passes.length === 0) await test('promise should pass', new Promise(resolve => { resolve('') })) -console.assert(errors.some(call => /.*promise should pass.*/.test(call[0])) === false, `good promise errored`) -console.assert(logs.some(call => /.*promise should pass.*/.test(call[0])) === true, `good promise not logged`) +console.assert(failures.some(call => /.*promise should pass.*/.test(call[0])) === false, `good promise errored`) +console.assert(passes.some(call => /.*promise should pass.*/.test(call[0])) === true, `good promise not logged`) await test('promise should fail', new Promise((resolve, reject) => { reject('FAILURE EXPECTED HERE') })) -console.assert(errors.some(call => /.*promise should fail.*/.test(call[0])) === true, `bad promise not errored`) -console.assert(logs.some(call => /.*promise should fail.*/.test(call[0])) === false, 'bad promise logged') +console.assert(failures.some(call => /.*promise should fail.*/.test(call[0])) === true, `bad promise not errored`) +console.assert(passes.some(call => /.*promise should fail.*/.test(call[0])) === false, 'bad promise logged') -await test('async should pass', async () => {}) -console.assert(errors.some(call => /.*async should pass.*/.test(call[0])) === false, 'good async errored') -console.assert(logs.some(call => /.*async should pass.*/.test(call[0])) === true, 'good async not logged') +await test('async should pass', async () => { }) +console.assert(failures.some(call => /.*async should pass.*/.test(call[0])) === false, 'good async errored') +console.assert(passes.some(call => /.*async should pass.*/.test(call[0])) === true, 'good async not logged') await test('async should fail', async () => { throw new Error('FAILURE EXPECTED HERE') }) -console.assert(errors.some(call => /.*async should fail.*/.test(call[0])) === true, 'bad async not errored') -console.assert(logs.some(call => /.*async should fail.*/.test(call[0])) === false, 'bad async logged') +console.assert(failures.some(call => /.*async should fail.*/.test(call[0])) === true, 'bad async not errored') +console.assert(passes.some(call => /.*async should fail.*/.test(call[0])) === false, 'bad async logged') -await test('function should pass', () => {}) -console.assert(errors.some(call => /.*function should pass.*/.test(call[0])) === false, 'good function errored') -console.assert(logs.some(call => /.*function should pass.*/.test(call[0])) === true, 'good function not logged') +await test('function should pass', () => { }) +console.assert(failures.some(call => /.*function should pass.*/.test(call[0])) === false, 'good function errored') +console.assert(passes.some(call => /.*function should pass.*/.test(call[0])) === true, 'good function not logged') await test('function should fail', 'FAILURE EXPECTED HERE') -console.assert(errors.some(call => /.*function should fail.*/.test(call[0])) === true, 'bad function not errored') -console.assert(logs.some(call => /.*function should fail.*/.test(call[0])) === false, 'bad function logged') +console.assert(failures.some(call => /.*function should fail.*/.test(call[0])) === true, 'bad function not errored') +console.assert(passes.some(call => /.*function should fail.*/.test(call[0])) === false, 'bad function logged') console.log(`> TEST RUNNER CHECK DONE <`) console.log(` `) -export function skip (name, ...args) { - log(`SKIP: ${name}`) +export function skip (name) { + console.log(`%cSKIP `, 'color:blue', name) +} + +export function suite (name, fn) { + console.group(`%c=>%c ${name}`, 'font-weight:bold;color:green', 'font-weight:bold;color:unset') + if (fn.constructor.name === 'AsyncFunction') fn = fn() + if (typeof fn === 'function') fn = new Promise(resolve => resolve(fn())) + fn.then(console.groupEnd) } export function test (name, fn) { - console.log(`=> Test: ${name}`) if (fn instanceof Promise) { try { return fn - .then(() => log(` -> PASS: ${name}`)) - .catch((err) => { error(` -> FAIL: ${name}: ${err}`) }) + .then(() => pass(name)) + .catch((err) => { fail(`${name}: ${err}`) }) } catch (err) { - error(` -> FAIL: ${name}: ${err.message}`) - error(err) + fail(`${name}: ${err.message}`) + fail(err) } } else if (fn?.constructor?.name === 'AsyncFunction') { try { return fn() - .then(() => log(` -> PASS: ${name}`)) - .catch((err) => error(` -> FAIL: ${name}: ${err.message}`)) + .then(() => pass(name)) + .catch((err) => fail(`${name}: ${err.message}`)) } catch (err) { - error(` -> FAIL: ${name}: ${err.message}`) - error(err) + fail(`${name}: ${err.message}`) + fail(err) } } else if (typeof fn === 'function') { try { fn() - log(` -> PASS: ${name}`) + pass(name) } catch (err) { - error(` -> FAIL: ${name}: ${err.message}`) - error(err) + fail(`${name}: ${err.message}`) + fail(err) } } else { - error(` -> FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`) + fail(`${name}: test cannot execute on ${typeof fn} ${fn}`) } } diff --git a/test.html b/test.html index 010cfaa..4b265bd 100644 --- a/test.html +++ b/test.html @@ -3,8 +3,8 @@ - - + +