mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 13:04:30 -08:00
69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
/*
|
|
For node.js tests
|
|
|
|
It forces node.js to load Response (which is loaded lazily)
|
|
|
|
Without this, `Response` loading code would be executed in record_io.js and
|
|
break test by calling `now()`
|
|
*/
|
|
globalThis.Response
|
|
|
|
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')
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|