Skip to content

Commit a1e8373

Browse files
committed
Merge pull request #570 from jurko-gospodnetic/fix-error-message-string-substitutions
Fix error message string substitutions
2 parents 068dc2b + 568fc1a commit a1e8373

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

lib/chai/utils/getMessage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ module.exports = function (obj, args) {
4343
if(typeof msg === "function") msg = msg();
4444
msg = msg || '';
4545
msg = msg
46-
.replace(/#{this}/g, objDisplay(val))
47-
.replace(/#{act}/g, objDisplay(actual))
48-
.replace(/#{exp}/g, objDisplay(expected));
46+
.replace(/#{this}/g, function () { return objDisplay(val); })
47+
.replace(/#{act}/g, function () { return objDisplay(actual); })
48+
.replace(/#{exp}/g, function () { return objDisplay(expected); });
4949

5050
return flagMsg ? flagMsg + ': ' + msg : msg;
5151
};

test/utilities.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ describe('utilities', function () {
342342
var obj = {};
343343
_.flag(obj, 'message', 'foo');
344344
expect(_.getMessage(obj, [])).to.contain('foo');
345+
});
346+
});
345347

348+
it('getMessage passed message as function', function () {
349+
chai.use(function (_chai, _) {
346350
var obj = {};
347351
var msg = function() { return "expected a to eql b"; }
348352
var negateMsg = function() { return "expected a not to eql b"; }
@@ -352,6 +356,67 @@ describe('utilities', function () {
352356
});
353357
});
354358

359+
it('getMessage template tag substitution', function () {
360+
chai.use(function (_chai, _) {
361+
var objName = 'trojan horse';
362+
var actualValue = 'an actual value';
363+
var expectedValue = 'an expected value';
364+
[
365+
// known template tags
366+
{
367+
template: 'one #{this} two',
368+
expected: 'one \'' + objName + '\' two'
369+
},
370+
{
371+
template: 'one #{act} two',
372+
expected: 'one \'' + actualValue + '\' two'
373+
},
374+
{
375+
template: 'one #{exp} two',
376+
expected: 'one \'' + expectedValue + '\' two'
377+
},
378+
// unknown template tag
379+
{
380+
template: 'one #{unknown} two',
381+
expected: 'one #{unknown} two'
382+
},
383+
// repeated template tag
384+
{
385+
template: '#{this}#{this}',
386+
expected: '\'' + objName + '\'\'' + objName + '\''
387+
},
388+
// multiple template tags in different order
389+
{
390+
template: '#{this}#{act}#{exp}#{act}#{this}',
391+
expected: '\'' + objName + '\'\'' + actualValue + '\'\'' + expectedValue + '\'\'' + actualValue + '\'\'' + objName + '\''
392+
},
393+
// immune to string.prototype.replace() `$` substitution
394+
{
395+
objName: '-$$-',
396+
template: '#{this}',
397+
expected: '\'-$$-\''
398+
},
399+
{
400+
actualValue: '-$$-',
401+
template: '#{act}',
402+
expected: '\'-$$-\''
403+
},
404+
{
405+
expectedValue: '-$$-',
406+
template: '#{exp}',
407+
expected: '\'-$$-\''
408+
}
409+
].forEach(function (config) {
410+
config.objName = config.objName || objName;
411+
config.actualValue = config.actualValue || actualValue;
412+
config.expectedValue = config.expectedValue || expectedValue;
413+
var obj = {_obj: config.actualValue};
414+
_.flag(obj, 'object', config.objName);
415+
expect(_.getMessage(obj, [null, config.template, null, config.expectedValue])).to.equal(config.expected);
416+
});
417+
});
418+
});
419+
355420
it('inspect with custom object-returning inspect()s', function () {
356421
chai.use(function (_chai, _) {
357422
var obj = {

0 commit comments

Comments
 (0)