From 872bb767b9b649b7964a86fa56f00252ac91bc7a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 18 Jul 2025 09:06:30 -0700 Subject: [PATCH] Spruce up test output. --- index.html | 53 ++++++++++++++++++++++++++++++++++------------ test/GLOBALS.mjs | 14 +++++++----- test/test.main.mjs | 4 ++++ 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/index.html b/index.html index f193df5..c9627e6 100644 --- a/index.html +++ b/index.html @@ -33,8 +33,36 @@ SPDX-License-Identifier: GPL-3.0-or-later return hex.slice(0, size) } const output = document.getElementById('output') + + const consoleError = console.error + const consoleLog = console.log + + function consoleOverride () { + console.error = (...args) => { + let a = args.join() + a = a.replace('%cFAIL ,color:red,', 'FAIL ') + output.innerHTML += `
${a}
` + consoleError(...args) + window?.scrollTo(0, document.body.scrollHeight) + } + console.log = (...args) => { + let a = args.join() + a = a.replace('%cTEST RUNNER CHECK DONE,font-weight:bold', 'TEST RUNNER CHECK DONE ') + a = a.replace('%cPASS ,color:green,', 'PASS ') + a = a.replace('%cSKIP ,color:CornflowerBlue,', 'SKIP ') + a = a.replace('%cTESTING COMPLETE,color:orange;font-weight:bold', 'TESTING COMPLETE') + a = a.replace('%cPASS: ,color:green;font-weight:bold,', 'PASS ') + a = a.replace('%cFAIL: ,color:red;font-weight:bold,', 'FAIL ') + output.innerHTML += `
${a}
` + consoleLog(...args) + window?.scrollTo(0, document.body.scrollHeight) + } + } + consoleOverride() + const consoleGroup = console.group console.group = (...args) => { + consoleOverride() let a = args.join() a = a.replace('%cSKIP ,color:CornflowerBlue,', 'SKIP ') a = a.replace(',font-weight:bold', '') @@ -43,23 +71,22 @@ SPDX-License-Identifier: GPL-3.0-or-later consoleGroup(...args) window?.scrollTo(0, document.body.scrollHeight) } - const consoleError = console.error - console.error = (...args) => { + const consoleGroupCollapsed = console.groupCollapsed + console.groupCollapsed = (...args) => { + console.error = (...args) => consoleError(...args) + console.log = (...args) => consoleLog(...args) let a = args.join() - a = a.replace('%cFAIL ,color:red,', 'FAIL ') + a = a.replace('%cSKIP ,color:CornflowerBlue,', 'SKIP ') + a = a.replace(',font-weight:bold', '') + a = a.replace('%c', '') output.innerHTML += `
${a}
` - consoleError(...args) + consoleGroupCollapsed(...args) window?.scrollTo(0, document.body.scrollHeight) } - const consoleLog = console.log - console.log = (...args) => { - let a = args.join() - a = a.replace('%cTEST RUNNER CHECK DONE,font-weight:bold', 'TEST RUNNER CHECK DONE ') - a = a.replace('%cPASS ,color:green,', 'PASS ') - a = a.replace('%cSKIP ,color:CornflowerBlue,', 'SKIP ') - a = a.replace('%cTESTING COMPLETE,color:orange;font-weight:bold', 'TESTING COMPLETE') - output.innerHTML += `
${a}
` - consoleLog(...args) + const consoleGroupEnd = console.groupEnd + console.groupEnd = () => { + consoleGroupEnd() + consoleOverride() window?.scrollTo(0, document.body.scrollHeight) } })() diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 3973857..5435b17 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -103,8 +103,8 @@ export function stats (times) { } } -const failures = [] -const passes = [] +export const failures = [] +export const passes = [] function fail (err) { failures.push(err.message) console.error(`%cFAIL `, 'color:red', err.message, err.cause) @@ -114,7 +114,7 @@ function pass (name) { console.log(`%cPASS `, 'color:green', name) } -await suite('TEST RUNNER CHECK', async () => { +await suite('TEST RUNNER CHECK', { collapse: true }, async () => { await new Promise(r => setTimeout(r, 0)) console.assert(failures.length === 0) @@ -144,8 +144,10 @@ await suite('TEST RUNNER CHECK', async () => { console.assert(failures.some(call => /.*function should fail.*/.test(call)) === true, 'bad function not errored') console.assert(passes.some(call => /.*function should fail.*/.test(call)) === false, 'bad function logged') - console.log(`%cTEST RUNNER CHECK DONE`, 'font-weight:bold') + failures.splice(0) + passes.splice(0) }) +console.log(`%cTEST RUNNER CHECK DONE`, 'font-weight:bold') export function suite (name, opts, fn) { if (fn === undefined) fn = opts @@ -157,7 +159,9 @@ export function suite (name, opts, fn) { } if (fn?.constructor?.name === 'AsyncFunction') fn = fn() if (typeof fn === 'function') fn = new Promise(resolve => resolve(fn())) - console.group(`%c${name}`, 'font-weight:bold') + opts?.collapse + ? console.groupCollapsed(`%c${name}`, 'font-weight:bold') + : console.group(`%c${name}`, 'font-weight:bold') await fn console.groupEnd() }) diff --git a/test/test.main.mjs b/test/test.main.mjs index ff50320..e954753 100644 --- a/test/test.main.mjs +++ b/test/test.main.mjs @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: 2025 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later +import { failures, passes } from './GLOBALS.mjs' + import './test.blake2b.mjs' import './test.blocks.mjs' import './test.calculate-pow.mjs' @@ -14,3 +16,5 @@ import './test.refresh-accounts.mjs' import './test.tools.mjs' console.log('%cTESTING COMPLETE', 'color:orange;font-weight:bold') +console.log('%cPASS: ', 'color:green;font-weight:bold', passes.length) +console.log('%cFAIL: ', 'color:red;font-weight:bold', failures.length) -- 2.47.3