From: Chris Duncan Date: Fri, 6 Dec 2024 20:26:48 +0000 (-0800) Subject: Move test runner into globals file so it is set up for every test. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8f869c6efc751104411676509d3ac966624b86c5;p=libnemo.git Move test runner into globals file so it is set up for every test. --- diff --git a/GLOBALS.mjs b/GLOBALS.mjs index 0ed94e8..dd8b76f 100644 --- a/GLOBALS.mjs +++ b/GLOBALS.mjs @@ -16,35 +16,83 @@ if (globalThis.sessionStorage == null) { }) } +const errors = [] +const logs = [] +function error (...args) { + errors.push(args) + console.error(...args) +} +function log (...args) { + logs.push(args) + console.log(...args) +} + +/** +* Who watches the watchers? +*/ +console.assert(errors.length === 0) +console.assert(logs.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`) + +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') + +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 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') + +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 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.log(`> TEST RUNNER CHECK DONE <`) +console.log(` `) + export function skip (name, ...args) { - console.log(`SKIP: ${name}`) + log(`SKIP: ${name}`) } export function test (name, fn) { if (fn instanceof Promise) { try { - fn.then(() => console.log(`PASS: ${name}`)) - .catch((err) => console.error(`FAIL: ${name}: ${err.message}`)) + return fn + .then(() => log(`PASS: ${name}`)) + .catch((err) => { error(`FAIL: ${name}: ${err}`) }) } catch (err) { - console.error(`FAIL: ${name}: ${err.message}`) - console.error(err) + error(`FAIL: ${name}: ${err.message}`) + error(err) } - } else if (fn.constructor.name === 'AsyncFunction') { + } else if (fn?.constructor?.name === 'AsyncFunction') { try { - fn().then(() => console.log(`PASS: ${name}`)) - .catch((err) => console.error(`FAIL: ${name}: ${err.message}`)) + return fn() + .then(() => log(`PASS: ${name}`)) + .catch((err) => error(`FAIL: ${name}: ${err.message}`)) } catch (err) { - console.error(`FAIL: ${name}: ${err.message}`) - console.error(err) + error(`FAIL: ${name}: ${err.message}`) + error(err) } - } else { + } else if (typeof fn === 'function') { try { fn() - console.log(`PASS: ${name}`) + log(`PASS: ${name}`) } catch (err) { - console.error(`FAIL: ${name}: ${err.message}`) - console.error(err) + error(`FAIL: ${name}: ${err.message}`) + error(err) } + } else { + error(`FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`) } } diff --git a/test/main.mjs b/test/main.mjs index e41b08b..b9c4203 100644 --- a/test/main.mjs +++ b/test/main.mjs @@ -1,7 +1,6 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -import './test.test.mjs' import './create-wallet.test.mjs' import './derive-accounts.test.mjs' import './import-wallet.test.mjs' @@ -12,5 +11,5 @@ import './sign-blocks.test.mjs' import './tools.test.mjs' document.addEventListener('DOMContentLoaded', () => { - console.log('> TESTING COMPLETE <') + console.log('> TESTING COMPLETE <') }) diff --git a/test/test.test.mjs b/test/test.test.mjs deleted file mode 100644 index 25bf413..0000000 --- a/test/test.test.mjs +++ /dev/null @@ -1,50 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Chris Duncan -// SPDX-License-Identifier: GPL-3.0-or-later - -'use strict' - -import { assert, skip, test } from '#GLOBALS.mjs' - -function testTheTests () { - // Replace console.log with stub implementation. - const console_log = console.log - const calls = [] - console.log = (...args) => { - calls.push(args) - console_log(...args) - } - - try { - console.assert(calls.length == 0) - - test('good promise', new Promise(resolve => resolve())) - console.assert(calls.length == 1) - console.assert(calls[0][0] === 'PASS: good promise') - - test('bad promise', new Promise((resolve, reject) => reject())) - console.assert(calls.length == 2) - console.assert(calls[1][0] === 'FAIL: bad promise') - - test('good async', async () => { }) - console.assert(calls.length == 3) - console.assert(calls[2][0] === 'PASS: good async') - - test('bad async', async () => { throw new Error('msg') }) - console.assert(calls.length == 4) - console.assert(calls[3][0] === 'PASS: bad async: msg') - - test('good function', () => { }) - console.assert(calls.length == 5) - console.assert(calls[4][0] === 'PASS: good function') - - test('bad function', null) - console.assert(calls.length == 6) - console.assert(calls[5][0] === 'FAIL: bad function') - - } catch (error) { - console.error(error) - } finally { - // Restore original implementation after testing. - console.log = console_log - } -}