diff --git a/bin/cli.js b/bin/cli.js index cd1152d..b168460 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -import Cli from '../lib/cli.js' +import TestRunner from 'test-runner' -const cli = new Cli() -cli.start(process.argv.slice(2)) +const runner = new TestRunner() +runner.start(process.argv.slice(2)) diff --git a/index.js b/index.js index 5fe5170..ca1f3d5 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ +import Test from './lib/test.js' +import ansi from 'ansi-escape-sequences' +import { pathToFileURL } from 'node:url' + class TestRunner { tests - constructor (tests = []) { + constructor (tests) { this.tests = tests } @@ -19,6 +23,41 @@ class TestRunner { } return result } + + async start (files) { + const tests = [] + + function createTests (arr, map, file) { + for (const [name, testFn] of map) { + const test = new Test(name, testFn) + test.data.file = file + tests.push(test) + } + } + + for (const file of files) { + const importPath = pathToFileURL(file).href + const testModule = await import(importPath) + if (!testModule) { + throw new Error(`File did not export any tests: ${file}`) + } + if (testModule.skip && testModule.skip.size) { + for (const [name] of testModule.skip) { + console.log(`- ${ansi.format(name, ['grey'])}`) + } + } + if (testModule.only && testModule.only.size) { + createTests(tests, testModule.only, file) + } else if (testModule.test && testModule.test.size) { + createTests(tests, testModule.test, file) + } + } + + this.tests = tests + for await (const test of this.run()) { + console.log(`${ansi.format('✔', ['green'])} ${ansi.format(test.data.file, ['magenta'])} ${test.name}`) + } + } } export default TestRunner diff --git a/lib/cli.js b/lib/cli.js deleted file mode 100644 index 36e3e44..0000000 --- a/lib/cli.js +++ /dev/null @@ -1,47 +0,0 @@ -import TestRunner from 'test-runner' -import Test from './test.js' -import ansi from 'ansi-escape-sequences' -import { pathToFileURL } from 'node:url' - -class Cli { - async start (argv) { - const config = { - files: argv - } - const tests = [] - - function createTests (arr, map, file) { - for (const [name, testFn] of map) { - const test = new Test(name, testFn) - test.data.file = file - tests.push(test) - } - } - - for (const file of config.files) { - const importPath = pathToFileURL(file).href - const testModule = await import(importPath) - if (!testModule) { - throw new Error(`File did not export any tests: ${file}`) - } - if (testModule.skip && testModule.skip.size) { - for (const [name] of testModule.skip) { - console.log(`- ${ansi.format(name, ['grey'])}`) - } - } - if (testModule.only && testModule.only.size) { - createTests(tests, testModule.only, file) - } else if (testModule.test && testModule.test.size) { - createTests(tests, testModule.test, file) - } - } - - const runner = new TestRunner(tests) - for await (const test of runner.run()) { - console.log(`${ansi.format('✔', ['green'])} ${ansi.format(test.data.file, ['magenta'])} ${test.name}`) - } - return runner - } -} - -export default Cli diff --git a/lib/test.js b/lib/test.js index 0b9c840..b98f7f0 100644 --- a/lib/test.js +++ b/lib/test.js @@ -2,7 +2,6 @@ class Test { name testFn result - state data = {} // optional associated metadata, consumed by runner user (e.g. to store the test file name) not the runner itself constructor (name, testFn, options = {}) { diff --git a/test/test.js b/test/test.js index 6355a7f..8bf8e28 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,5 @@ import TestRunner from 'test-runner' import Test from '../lib/test.js' -import Cli from '../lib/cli.js' import { strict as a } from 'assert' /* Node.js version 12 compatible - no module-level await. */ @@ -21,7 +20,7 @@ one() /* Cli loads and runs a test file */ async function cli () { - const cli = new Cli() - await cli.start(['./test/fixture/one.js']) + const runner = new TestRunner() + await runner.start(['./test/fixture/one.js']) } cli()