From 32ac96adc52503c607488ef888de7211b671604d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Mon, 5 Dec 2022 17:07:06 -0500 Subject: [PATCH] Move all code to lib folder --- index.js | 62 ++----------------- lib/metaltest.js | 57 +++++++++++++++++ runifmain.js => lib/runifmain.js | 0 stackparser.js => lib/stackparser.js | 0 .../stackparser.test.js | 2 +- suite.js => lib/suite.js | 0 reporter/errorreporter.js | 2 +- reporter/linereporter.js | 2 +- test.js | 2 +- 9 files changed, 65 insertions(+), 62 deletions(-) create mode 100644 lib/metaltest.js rename runifmain.js => lib/runifmain.js (100%) rename stackparser.js => lib/stackparser.js (100%) rename stackparser.test.js => lib/stackparser.test.js (89%) rename suite.js => lib/suite.js (100%) diff --git a/index.js b/index.js index 9f31037..419fd40 100644 --- a/index.js +++ b/index.js @@ -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 { runifmain } from './runifmain.js' -export { suite } from './suite.js' +export { runifmain } from './lib/runifmain.js' +export { suite } from './lib/suite.js' + +import { metaltest } from './lib/metaltest.js' export { metaltest } export default metaltest \ No newline at end of file diff --git a/lib/metaltest.js b/lib/metaltest.js new file mode 100644 index 0000000..2fdecdd --- /dev/null +++ b/lib/metaltest.js @@ -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 } \ No newline at end of file diff --git a/runifmain.js b/lib/runifmain.js similarity index 100% rename from runifmain.js rename to lib/runifmain.js diff --git a/stackparser.js b/lib/stackparser.js similarity index 100% rename from stackparser.js rename to lib/stackparser.js diff --git a/stackparser.test.js b/lib/stackparser.test.js similarity index 89% rename from stackparser.test.js rename to lib/stackparser.test.js index b0f5cb3..45c3795 100644 --- a/stackparser.test.js +++ b/lib/stackparser.test.js @@ -1,5 +1,5 @@ import assert from 'node:assert/strict' -import { metaltest, consolereporter } from './index.js' +import { metaltest, consolereporter } from '../index.js' import { stackParser } from './stackparser.js' const test = metaltest('Stack parser') diff --git a/suite.js b/lib/suite.js similarity index 100% rename from suite.js rename to lib/suite.js diff --git a/reporter/errorreporter.js b/reporter/errorreporter.js index 0792ae3..18b37cf 100644 --- a/reporter/errorreporter.js +++ b/reporter/errorreporter.js @@ -18,7 +18,7 @@ const errorreporter = () => { return report } -import { stackParser } from '../stackparser.js' +import { stackParser } from '../lib/stackparser.js' const prettyError = (error) => { if (typeof error == 'string') { log(chalk.yellowBright(error)) diff --git a/reporter/linereporter.js b/reporter/linereporter.js index 7e3b564..13fcdfc 100644 --- a/reporter/linereporter.js +++ b/reporter/linereporter.js @@ -1,5 +1,5 @@ import chalk from 'chalk' -import { stackParser } from '../stackparser.js' +import { stackParser } from '../lib/stackparser.js' const log = (...args) => console.log(...args) diff --git a/test.js b/test.js index 55c9c8e..eecb74a 100644 --- a/test.js +++ b/test.js @@ -17,7 +17,7 @@ test('throw', () => { export { test } -import { runifmain } from './runifmain.js' +import { runifmain } from './lib/runifmain.js' await runifmain(import.meta, async () => { const stats = await test.run(consolereporter()) assert.equal(count, 4)