diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 40713ef6..a3f303b6 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3495,9 +3495,19 @@ getJasmineRequireObj().Expectation = function(j$) { } ContextAddingFilter.prototype.modifyFailureMessage = function(msg) { - return this.message + ': ' + msg; + var nl = msg.indexOf('\n'); + + if (nl === -1) { + return this.message + ': ' + msg; + } else { + return this.message + ':\n' + indent(msg); + } }; + function indent(s) { + return s.replace(/^/gm, ' '); + } + return { factory: function(options) { return new Expectation(options || {}); diff --git a/spec/core/ExpectationSpec.js b/spec/core/ExpectationSpec.js index 45f69eb5..599b0558 100644 --- a/spec/core/ExpectationSpec.js +++ b/spec/core/ExpectationSpec.js @@ -589,6 +589,32 @@ describe('Expectation', function() { ); }); + it('indents a multiline failure message', function() { + var matchers = { + toFoo: function() { + return { + compare: function() { + return { pass: false, message: 'a\nmultiline\nmessage' }; + } + }; + } + }, + addExpectationResult = jasmine.createSpy('addExpectationResult'), + expectation = jasmineUnderTest.Expectation.factory({ + customMatchers: matchers, + actual: 'an actual', + addExpectationResult: addExpectationResult + }), + actualMessage; + + expectation.withContext('Some context').toFoo('hello'); + + actualMessage = addExpectationResult.calls.argsFor(0)[1].message; + expect(actualMessage).toEqual( + 'Some context:\n a\n multiline\n message' + ); + }); + it('prepends the context to a custom failure message from a function', function() { var matchers = { toFoo: function() { diff --git a/src/core/Expectation.js b/src/core/Expectation.js index add788cb..37e1b7d6 100644 --- a/src/core/Expectation.js +++ b/src/core/Expectation.js @@ -176,9 +176,19 @@ getJasmineRequireObj().Expectation = function(j$) { } ContextAddingFilter.prototype.modifyFailureMessage = function(msg) { - return this.message + ': ' + msg; + var nl = msg.indexOf('\n'); + + if (nl === -1) { + return this.message + ': ' + msg; + } else { + return this.message + ':\n' + indent(msg); + } }; + function indent(s) { + return s.replace(/^/gm, ' '); + } + return { factory: function(options) { return new Expectation(options || {});