diff --git a/consolereporter.js b/consolereporter.js index d5186e5..f6cac23 100644 --- a/consolereporter.js +++ b/consolereporter.js @@ -1,4 +1,5 @@ import chalk from 'chalk' + const log = (...args) => process.stdout.write(...args) const consolereporter = () => { @@ -28,6 +29,7 @@ const consolereporter = () => { return report } +import { stackParser } from './stackparser.js' const prettyError = (error) => { if (typeof error == 'string') { log(chalk.yellowBright(error)) @@ -35,22 +37,14 @@ const prettyError = (error) => { return } - const msg = error.stack - const i = msg.indexOf('\n') + const stack = stackParser(error.stack) + log(chalk.yellowBright(stack.title) + '\n') - const title = msg.slice(0, i) - log(chalk.yellowBright(title) + '\n') + if (stack.diff != '\n ') + log(chalk.white(stack.diff)) - const at = msg.indexOf(' at ') - const diff = msg.slice(i + 1, at - 1) - if (diff != '\n ') - log(chalk.white(diff)) - - const atEnd = msg.slice(at).indexOf('\n') - const where = msg.slice(at + 3, at + atEnd) - - if (where != '') - log(chalk.gray(where) + '\n') + if (stack.where != '') + log(chalk.gray(stack.where) + '\n') } export { consolereporter } \ No newline at end of file diff --git a/linereporter.js b/linereporter.js index 2d1e5dd..e52b167 100644 --- a/linereporter.js +++ b/linereporter.js @@ -1,4 +1,5 @@ import chalk from 'chalk' +import { stackParser } from './stackparser.js' const linereporter = () => { const lines = [] @@ -22,8 +23,8 @@ const linereporter = () => { if (testFail.length == 1) { console.log('Lines in error', chalk.redBright(lines)) - const { error: { stack: e } } = testFail[0] - console.log(e.slice(e.indexOf('\n') + 1, e.indexOf(' at') - 1)); + const stack = stackParser(testFail[0].error.stack) + console.log(stack.diff) } } } diff --git a/stackparser.js b/stackparser.js new file mode 100644 index 0000000..6b56567 --- /dev/null +++ b/stackparser.js @@ -0,0 +1,15 @@ +const stackParser = (stack) => { + const endOfLine = stack.indexOf('\n') + + const title = stack.slice(0, endOfLine) + + const at = stack.indexOf(' at ') + const diff = stack.slice(endOfLine + 1, at - 1) + + const endOfLineAfterAt = stack.slice(at).indexOf('\n') + const where = stack.slice(at + 3, at + endOfLineAfterAt) + + return { title, diff, where } +} + +export { stackParser } \ No newline at end of file diff --git a/stackparser.test.js b/stackparser.test.js new file mode 100644 index 0000000..7d65b57 --- /dev/null +++ b/stackparser.test.js @@ -0,0 +1,19 @@ +import assert from 'node:assert/strict' +import { metaltest, consolereporter } from './index.js' +import { stackParser } from "./stackparser.js" + +const test = metaltest('Stack parser') + +test('Diff', () => { + try { + assert.deepEqual({ name: 'Name' }, { name: 'Name2' }) + } + catch (e) { + const stack = stackParser(e.stack) + assert.ok(stack.diff.includes("name: 'Name'")) + assert.ok(stack.diff.includes("name: 'Name2'")) + assert.ok(stack.where.includes('stackparser.test.js')) + } +}) + +await test.run(consolereporter())