Indent multiline failure messages in the output of withContext

This makes it easier to see where each failure message begins and ends.

Before:
   Some context: a
   multiline
   message

After:
   Some context:
       a
       multiline
       message
This commit is contained in:
Steve Gravrock
2019-09-28 12:31:00 -07:00
parent a497d0942a
commit 10dbf8be98
3 changed files with 48 additions and 2 deletions

View File

@@ -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 || {});

View File

@@ -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() {

View File

@@ -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 || {});