From fd31f38b3401e51b5c38c184733aa419c83889e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Wed, 7 Dec 2022 19:27:53 -0500 Subject: [PATCH] Use named capture group --- lib/stackparser.js | 9 ++------- lib/stackparser.test.js | 7 ++++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/stackparser.js b/lib/stackparser.js index db166e7..7906e1c 100644 --- a/lib/stackparser.js +++ b/lib/stackparser.js @@ -1,14 +1,9 @@ -const regAssertParser = /(.*?)\n+(.*?)\n+(( at.*?file:\/\/(.*?):(\d+):(\d+).*?\n).*)/s +const regAssertParser = /(?.*?)\n+(?<diff>.*?)\n+(?<stacktrace> (?<where>at.*?file:\/\/(?<file>.*?):(?<line>\d+):(?<column>\d+)\)?)\n.*)/s const stackParser = (stack) => { const m = stack.match(regAssertParser) - const info = m - .slice(1) - .map(x => x.trim()) - const [title, diff, stacktrace, where, file, line, column] = info - - return { title, diff, where, file, line, column, stacktrace } + return m.groups } export { stackParser } \ No newline at end of file diff --git a/lib/stackparser.test.js b/lib/stackparser.test.js index 600f5d6..c7f98da 100644 --- a/lib/stackparser.test.js +++ b/lib/stackparser.test.js @@ -11,9 +11,14 @@ test('Diff', () => { } catch (e) { const stack = stackParser(e.stack) + assert.equal(stack.title, 'AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:') assert.ok(stack.diff.includes("name: 'Name'")) assert.ok(stack.diff.includes("name: 'Name2'")) - assert.ok(stack.where.includes('stackparser.test.js')) + assert.equal(stack.where, 'at Object.fn (file:///home/taz/code/metaltest/lib/stackparser.test.js:10:12)') + assert.equal(stack.file, '/home/taz/code/metaltest/lib/stackparser.test.js') + assert.equal(stack.line, '10') + assert.equal(stack.column, '12') + assert.ok(stack.stacktrace.startsWith(' at Object.fn (file:///home/taz/code/metaltest/lib/stackparser.test.js:10:12)')) } })