From a1f694b9ebc3ad8e10965596e5efc3c0e8c9291d Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 24 Aug 2024 14:04:19 +0100 Subject: [PATCH] support this.data --- index.js | 18 ++++++++++++++++-- lib/test.js | 3 ++- test/fixture/two.js | 7 +++++++ test/test.js | 38 +++++++++++++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 test/fixture/two.js diff --git a/index.js b/index.js index ca1f3d5..f7a43c6 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) } } @@ -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, ' ')) + } } } } diff --git a/lib/test.js b/lib/test.js index b98f7f0..2055f68 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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 diff --git a/test/fixture/two.js b/test/fixture/two.js new file mode 100644 index 0000000..b7e1105 --- /dev/null +++ b/test/fixture/two.js @@ -0,0 +1,7 @@ +const test = new Map() + +test.set('two', function two () { + throw new Error('broken') +}) + +export { test } diff --git a/test/test.js b/test/test.js index 8bf8e28..2cce320 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () { @@ -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()