Add a stackparser

master
Frédéric Matte 2022-12-04 23:40:07 -05:00
parent 036f7a0563
commit 4c3cca0cd3
4 changed files with 45 additions and 16 deletions

View File

@ -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 }

View File

@ -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)
}
}
}

15
stackparser.js Normal file
View File

@ -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 }

19
stackparser.test.js Normal file
View File

@ -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())