Use named capture group
parent
898d3a7abc
commit
fd31f38b34
|
@ -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 }
|
|
@ -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)'))
|
||||
}
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue