Skip to content

Commit

Permalink
fix(adapter): don't duplicate multiline failure messages
Browse files Browse the repository at this point in the history
The current code to handle removing error messages for formatting does not
properly handle multiline error messages and results in the message being
repeated at the top of the stack trace. The code only searched for the message
in the first stack line, which meant it would never be found because the message
was multiple lines, which meant that the message would be pre-pended by the
message not in stack logic.

The existing multiline test actually triggers this behavior, but because it used
toMatch it only verified that the message was included, not that it only existed
once.
  • Loading branch information
mitchellwills authored and Jonathan Ginsburg committed May 18, 2022
1 parent 58d5d25 commit 6f56439
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,23 @@ function formatFailedStep (step) {

// Remove the message prior to processing the stack to prevent issues like
// https://github.com/karma-runner/karma-jasmine/issues/79
var stackframes = step.stack.split('\n')

// Jasmine filters empty newlines from the stack so remove them from the
// message before searching for it. Also, also remove the trailing newline (if
// present) so that when the message is put back on the top of the stack it
// doesn't introduce an extra newline.
var messageWithoutEmptyLines = step.message.replace(/\n+/g, '\n').replace(/\n$/, '')
var messageStartIndex = step.stack.indexOf(messageWithoutEmptyLines)
var firstNewlineIndex = step.stack.indexOf('\n')
var messageOnStack = null
if (stackframes[0].indexOf(step.message) !== -1) {
var stack = step.stack
if (messageStartIndex !== -1 && messageStartIndex < firstNewlineIndex) {
// Remove the message if it is in the stack string (eg Chrome)
messageOnStack = stackframes.shift()
messageOnStack = step.stack.substring(0, messageStartIndex + messageWithoutEmptyLines.length)
stack = step.stack.substring(messageStartIndex + messageWithoutEmptyLines.length)
}

var stackframes = stack.split('\n')
// Filter frames
var relevantStackFrames = getRelevantStackFrom(stackframes)
if (messageOnStack) {
Expand Down
15 changes: 13 additions & 2 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('jasmine adapter', function () {
stack: '@file.js:123\n'
}

expect(formatFailedStep(step)).toMatch(/^Jasmine fail message/)
expect(formatFailedStep(step)).toBe('Jasmine fail message\n@file.js:123')
})

it('should report message if no stack trace', function () {
Expand All @@ -383,6 +383,7 @@ describe('jasmine adapter', function () {

expect(formatFailedStep(step)).toBe('MESSAGE\nsource/controller.js:45')
})

it('should properly format message containing new-line characters', function () {
// FF does not have the message in the stack trace

Expand All @@ -392,7 +393,17 @@ describe('jasmine adapter', function () {
stack: 'Error: Jasmine fail\nmessage\n@file.js:123'
}

expect(formatFailedStep(step)).toMatch('Jasmine fail\nmessage\n@file.js:123')
expect(formatFailedStep(step)).toBe('Error: Jasmine fail\nmessage\n@file.js:123')
})

it('should properly format message containing repeated new-line characters', function () {
var step = {
passed: false,
message: 'Failed: 1. failure message\n\n\n2. another failure\n3. a third failure\n\n\n\n',
stack: 'Error: Failed: 1. failure message\n2. another failure\n3. a third failure\n at <Jasmine>\n at UserContext.<anonymous> (/base/jasmine/test.js:13:9)\n at <Jasmine>'
}

expect(formatFailedStep(step)).toBe('Error: Failed: 1. failure message\n2. another failure\n3. a third failure\n at <Jasmine>\n at UserContext.<anonymous> (/base/jasmine/test.js:13:9)\n at <Jasmine>')
})
})

Expand Down

0 comments on commit 6f56439

Please sign in to comment.