]> zoso.dev Git - libnemo.git/commitdiff
Move test runner into globals file so it is set up for every test.
authorChris Duncan <chris@zoso.dev>
Fri, 6 Dec 2024 20:26:48 +0000 (12:26 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 6 Dec 2024 20:26:48 +0000 (12:26 -0800)
GLOBALS.mjs
test/main.mjs
test/test.test.mjs [deleted file]

index 0ed94e809550dd3dfe190319bd9a770892a5c251..dd8b76f83994fd65dbc7015043edef52fbf3c162 100644 (file)
@@ -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}`)
        }
 }
 
index e41b08bbed5b5d87b48e5176ded0bca157247e52..b9c42039fca4653d9657d257738033948e24b558 100644 (file)
@@ -1,7 +1,6 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
 // 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 (file)
index 25bf413..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
-// 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
-       }
-}