Move all code to lib folder

master
Frédéric Matte 2022-12-05 17:07:06 -05:00
parent b8ee7685fc
commit 32ac96adc5
9 changed files with 65 additions and 62 deletions

View File

@ -1,61 +1,7 @@
import { nullreporter } from './reporter/nullreporter.js'
const metaltest = (title) => {
const suite = []
const only = []
const before = []
const after = []
let success = 0
let fail = 0
const testSuccess = []
const testFail = []
const runner = (name, fn) => {
suite.push({ name, fn })
}
runner.only = (name, fn) => { only.push({ name, fn }) }
runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) }
runner.run = async (...reporters) => {
const rs = reporters.map(r => Object.assign({}, nullreporter, r))
rs.forEach(r => r.start(title))
rs.forEach(r => r.before())
const tests = only.length ? only : suite
for (const test of tests) {
try {
for (const fn of before) await fn()
await test.fn()
success++
rs.forEach(r => r.success(test))
}
catch (e) {
fail++
testFail.push(Object.assign({}, test, { error: e }))
rs.forEach(r => r.fail(test, e))
}
}
for (const fn of after) await fn()
rs.forEach(r => r.after())
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
rs.forEach(r => r.end(stats))
return stats
}
return runner
}
export * from './reporter/index.js' export * from './reporter/index.js'
export { runifmain } from './runifmain.js' export { runifmain } from './lib/runifmain.js'
export { suite } from './suite.js' export { suite } from './lib/suite.js'
import { metaltest } from './lib/metaltest.js'
export { metaltest } export { metaltest }
export default metaltest export default metaltest

57
lib/metaltest.js Normal file
View File

@ -0,0 +1,57 @@
import { nullreporter } from '../reporter/nullreporter.js'
const metaltest = (title) => {
const suite = []
const only = []
const before = []
const after = []
let success = 0
let fail = 0
const testSuccess = []
const testFail = []
const runner = (name, fn) => {
suite.push({ name, fn })
}
runner.only = (name, fn) => { only.push({ name, fn }) }
runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) }
runner.run = async (...reporters) => {
const rs = reporters.map(r => Object.assign({}, nullreporter, r))
rs.forEach(r => r.start(title))
rs.forEach(r => r.before())
const tests = only.length ? only : suite
for (const test of tests) {
try {
for (const fn of before) await fn()
await test.fn()
success++
rs.forEach(r => r.success(test))
}
catch (e) {
fail++
testFail.push(Object.assign({}, test, { error: e }))
rs.forEach(r => r.fail(test, e))
}
}
for (const fn of after) await fn()
rs.forEach(r => r.after())
const stats = { title, success, fail, total: success + fail, testSuccess, testFail }
rs.forEach(r => r.end(stats))
return stats
}
return runner
}
export { metaltest }

View File

@ -1,5 +1,5 @@
import assert from 'node:assert/strict' import assert from 'node:assert/strict'
import { metaltest, consolereporter } from './index.js' import { metaltest, consolereporter } from '../index.js'
import { stackParser } from './stackparser.js' import { stackParser } from './stackparser.js'
const test = metaltest('Stack parser') const test = metaltest('Stack parser')

View File

@ -18,7 +18,7 @@ const errorreporter = () => {
return report return report
} }
import { stackParser } from '../stackparser.js' import { stackParser } from '../lib/stackparser.js'
const prettyError = (error) => { const prettyError = (error) => {
if (typeof error == 'string') { if (typeof error == 'string') {
log(chalk.yellowBright(error)) log(chalk.yellowBright(error))

View File

@ -1,5 +1,5 @@
import chalk from 'chalk' import chalk from 'chalk'
import { stackParser } from '../stackparser.js' import { stackParser } from '../lib/stackparser.js'
const log = (...args) => console.log(...args) const log = (...args) => console.log(...args)

View File

@ -17,7 +17,7 @@ test('throw', () => {
export { test } export { test }
import { runifmain } from './runifmain.js' import { runifmain } from './lib/runifmain.js'
await runifmain(import.meta, async () => { await runifmain(import.meta, async () => {
const stats = await test.run(consolereporter()) const stats = await test.run(consolereporter())
assert.equal(count, 4) assert.equal(count, 4)