diff --git a/src/adapter.js b/src/adapter.js index a64bf7b..5a06773 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -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) { diff --git a/test/adapter.spec.js b/test/adapter.spec.js index de0d308..9f91bf6 100644 --- a/test/adapter.spec.js +++ b/test/adapter.spec.js @@ -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 () { @@ -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 @@ -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 \n at UserContext. (/base/jasmine/test.js:13:9)\n at ' + } + + expect(formatFailedStep(step)).toBe('Error: Failed: 1. failure message\n2. another failure\n3. a third failure\n at \n at UserContext. (/base/jasmine/test.js:13:9)\n at ') }) })