diff --git a/test/run.js b/test/run.js index d679217..d4a9e7e 100644 --- a/test/run.js +++ b/test/run.js @@ -1,4 +1,6 @@ import {tests} from './test.js' -import {run} from './utils.js' + +// external +import {run} from './run_utils.js' await run(tests) diff --git a/test/run_utils.js b/test/run_utils.js new file mode 100644 index 0000000..df7c261 --- /dev/null +++ b/test/run_utils.js @@ -0,0 +1,58 @@ +export const run = tests => { + // Runs test, return failure or null if not failed + const run_test = t => { + return Promise.resolve().then(t.test) + .then(() => null) + .catch(e => { + if(globalThis.process != null) { + // In node.js runner, fail fast + console.error('Failed: ' + t.message) + throw e + } else { + return e + } + }) + } + + // If not run in node, then dont apply filter + const filter = globalThis.process && globalThis.process.argv[2] + + if(filter == null) { + + const only = tests.find(t => t.only) + const tests_to_run = only == null ? tests : [only] + + // Exec each test. After all tests are done, we rethrow first error if + // any. So we will mark root calltree node if one of tests failed + return tests_to_run.reduce( + (failureP, t) => + Promise.resolve(failureP).then(failure => + run_test(t).then(next_failure => failure ?? next_failure) + ) + , + null + ).then(failure => { + + if(failure != null) { + throw failure + } else { + if(globalThis.process != null) { + console.log('Ok') + } + } + + }) + + } else { + const test = tests.find(t => t.message.includes(filter)) + if(test == null) { + throw new Error('test not found') + } else { + return run_test(test).then(() => { + if(globalThis.process != null) { + console.log('Ok') + } + }) + } + } +} diff --git a/test/utils.js b/test/utils.js index 8010396..a3a9626 100644 --- a/test/utils.js +++ b/test/utils.js @@ -234,66 +234,3 @@ export const test = (message, test, only = false) => { } export const test_only = (message, t) => test(message, t, true) - -// Wrap to Function constructor to hide from calltree view -// TODO in calltree view, hide fn which has special flag set (see -// filter_calltree) - -export const run = Object.defineProperty(new Function('tests', ` - // Runs test, return failure or null if not failed - const run_test = t => { - return Promise.resolve().then(t.test) - .then(() => null) - .catch(e => { - if(globalThis.process != null) { - // In node.js runner, fail fast - console.error('Failed: ' + t.message) - throw e - } else { - return e - } - }) - } - - // If not run in node, then dont apply filter - const filter = globalThis.process && globalThis.process.argv[2] - - if(filter == null) { - - const only = tests.find(t => t.only) - const tests_to_run = only == null ? tests : [only] - - // Exec each test. After all tests are done, we rethrow first error if - // any. So we will mark root calltree node if one of tests failed - return tests_to_run.reduce( - (failureP, t) => - Promise.resolve(failureP).then(failure => - run_test(t).then(next_failure => failure ?? next_failure) - ) - , - null - ).then(failure => { - - if(failure != null) { - throw failure - } else { - if(globalThis.process != null) { - console.log('Ok') - } - } - - }) - - } else { - const test = tests.find(t => t.message.includes(filter)) - if(test == null) { - throw new Error('test not found') - } else { - return run_test(test).then(() => { - if(globalThis.process != null) { - console.log('Ok') - } - }) - } - } -`), 'name', {value: 'run'})