})
}
+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}`)
}
}
+++ /dev/null
-// 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
- }
-}