Skip to content

Commit

Permalink
support this.data
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Aug 24, 2024
1 parent cac4d1e commit a1f694b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import Test from './lib/test.js'
import ansi from 'ansi-escape-sequences'
import { pathToFileURL } from 'node:url'
import os from 'node:os'
import util from 'node:util'

/* TODO: Factor out node-specific code */

function indent (input, indentWith) {
const lines = input.split(os.EOL).map(line => {
return indentWith + line
})
return lines.join(os.EOL)
}

class TestRunner {
tests
Expand Down Expand Up @@ -30,7 +41,7 @@ class TestRunner {
function createTests (arr, map, file) {
for (const [name, testFn] of map) {
const test = new Test(name, testFn)
test.data.file = file
test.metadata.file = file
tests.push(test)
}
}
Expand All @@ -55,7 +66,10 @@ class TestRunner {

this.tests = tests
for await (const test of this.run()) {
console.log(`${ansi.format('✔', ['green'])} ${ansi.format(test.data.file, ['magenta'])} ${test.name}`)
console.log(`${ansi.format('✔', ['green'])} ${ansi.format(test.metadata.file, ['magenta'])} ${test.name}`)
if (test.data) {
console.log(indent(os.EOL + util.inspect(test.data, { colors: true }) + os.EOL, ' '))
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Test {
name
testFn
result
data = {} // optional associated metadata, consumed by runner user (e.g. to store the test file name) not the runner itself
metadata = {} // optional associated metadata, consumed by runner user (e.g. to store the test file name) not the runner itself
data // user context data displayed in the output alongside the text result

constructor (name, testFn, options = {}) {
this.name = name
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = new Map()

test.set('two', function two () {
throw new Error('broken')
})

export { test }
38 changes: 33 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { strict as a } from 'assert'

/* Node.js version 12 compatible - no module-level await. */

/* Sync tess passes, storing the result */
/* Sync test passes, storing the result */
async function one () {
const actuals = []
const test1 = new Test('one', function one () {
Expand All @@ -18,9 +18,37 @@ async function one () {
}
one()

/* Cli loads and runs a test file */
async function cli () {
/* Sync test fails, crashing the process */
async function syncFail () {
const actuals = []
const test1 = new Test('one', function one () {
actuals.push('one')
throw new Error('broken')
})
const runner = new TestRunner([test1])
try {
await runner.runAll()
throw new Error('Should not reach here')
} catch (err) {
a.equal(err.message, 'broken')
a.equal(test1.result, undefined)
a.deepEqual(actuals, ['one'])
}
}
syncFail()

{ /* Cli loads and runs a test file, test passes */
const runner = new TestRunner()
runner.start(['./test/fixture/one.js'])
}

{ /* Cli loads and runs a test file, test fails */
const runner = new TestRunner()
await runner.start(['./test/fixture/one.js'])
runner.start(['./test/fixture/two.js'])
.then(() => {
throw new Error('Should not reach here')
})
.catch(err => {
a.equal(err.message, 'broken')
})
}
cli()

0 comments on commit a1f694b

Please sign in to comment.