From 0a3d08244a20f0481459b7e7d9d651bfb0aeca97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Tue, 6 Dec 2022 12:58:30 -0500 Subject: [PATCH] Use regex to parse AssertionError --- lib/stackparser.js | 17 ++++++++--------- reporter/errorreporter.js | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/stackparser.js b/lib/stackparser.js index 6b56567..54db969 100644 --- a/lib/stackparser.js +++ b/lib/stackparser.js @@ -1,15 +1,14 @@ +const regAssertParser = /(.*?)\n+(.*?)\n+(( at.*?file:\/\/(.*?):(\d+):(\d+).*?\)).*)/s + const stackParser = (stack) => { - const endOfLine = stack.indexOf('\n') + const m = stack.match(regAssertParser) - const title = stack.slice(0, endOfLine) + const info = m + .slice(1) + .map(x => x.trim()) + const [title, diff, stacktrace, where, file, line, column] = info - 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 } + return { title, diff, where, file, line, column, stacktrace } } export { stackParser } \ No newline at end of file diff --git a/reporter/errorreporter.js b/reporter/errorreporter.js index 18b37cf..c6741a8 100644 --- a/reporter/errorreporter.js +++ b/reporter/errorreporter.js @@ -29,8 +29,8 @@ const prettyError = (error) => { const stack = stackParser(error.stack) log(chalk.yellowBright(stack.title) + '\n') - if (stack.diff != '\n ') - log(chalk.white(stack.diff)) + if (stack.diff != '') + log(chalk.white(stack.diff) + '\n') if (stack.where != '') log(chalk.gray(stack.where) + '\n')