Use named capture group

master
Frédéric Matte 2022-12-07 19:27:53 -05:00
parent 898d3a7abc
commit fd31f38b34
2 changed files with 8 additions and 8 deletions

View File

@ -1,14 +1,9 @@
const regAssertParser = /(.*?)\n+(.*?)\n+(( at.*?file:\/\/(.*?):(\d+):(\d+).*?\n).*)/s
const regAssertParser = /(?<title>.*?)\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 }

View File

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