Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Aug 23, 2024
1 parent 0cc27b7 commit cac4d1e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 55 deletions.
6 changes: 3 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -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))
41 changes: 40 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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
Expand Down
47 changes: 0 additions & 47 deletions lib/cli.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
5 changes: 2 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -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()

0 comments on commit cac4d1e

Please sign in to comment.