Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always honor custom message #947

Merged
merged 1 commit into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 76 additions & 43 deletions lib/chai/core/assertions.js
Expand Up @@ -299,9 +299,10 @@ module.exports = function (chai, _) {
}

function include (val, msg) {
if (msg) flag(this, 'message', msg);

_.expectTypes(this, ['array', 'object', 'string'], flag(this, 'ssfi'));

if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, objType = _.type(obj).toLowerCase()
, isDeep = flag(this, 'deep')
Expand Down Expand Up @@ -535,8 +536,11 @@ module.exports = function (chai, _) {
Assertion.addProperty('empty', function () {
var val = flag(this, 'object')
, ssfi = flag(this, 'ssfi')
, flagMsg = flag(this, 'message')
, itemsCount;

flagMsg = flagMsg ? flagMsg + ': ' : '';

switch (_.type(val).toLowerCase()) {
case 'array':
case 'string':
Expand All @@ -549,17 +553,17 @@ module.exports = function (chai, _) {
case 'weakmap':
case 'weakset':
throw new AssertionError(
'.empty was passed a weak collection',
flagMsg + '.empty was passed a weak collection',
undefined,
ssfi
);
case 'function':
var msg = '.empty was passed a function ' + _.getName(val);
var msg = flagMsg + '.empty was passed a function ' + _.getName(val);
throw new AssertionError(msg.trim(), undefined, ssfi);
default:
if (val !== Object(val)) {
throw new AssertionError(
'.empty was passed non-string primitive ' + _.inspect(val),
flagMsg + '.empty was passed non-string primitive ' + _.inspect(val),
undefined,
ssfi
);
Expand Down Expand Up @@ -706,17 +710,19 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (doLength) {
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
} else {
new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
}

if (typeof n !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the argument to above must be a number',
flagMsg + 'the argument to above must be a number',
undefined,
ssfi
);
Expand Down Expand Up @@ -772,17 +778,19 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (doLength) {
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
} else {
new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
}

if (typeof n !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the argument to least must be a number',
flagMsg + 'the argument to least must be a number',
undefined,
ssfi
);
Expand Down Expand Up @@ -838,17 +846,19 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (doLength) {
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
} else {
new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
}

if (typeof n !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the argument to below must be a number',
flagMsg + 'the argument to below must be a number',
undefined,
ssfi
);
Expand Down Expand Up @@ -904,17 +914,19 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, doLength = flag(this, 'doLength')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (doLength) {
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
} else {
new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
}

if (typeof n !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the argument to most must be a number',
flagMsg + 'the argument to most must be a number',
undefined,
ssfi
);
Expand Down Expand Up @@ -970,17 +982,19 @@ module.exports = function (chai, _) {
var obj = flag(this, 'object')
, range = start + '..' + finish
, doLength = flag(this, 'doLength')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (doLength) {
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
} else {
new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
}

if (typeof start !== 'number' || typeof finish !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the arguments to within must be numbers',
flagMsg + 'the arguments to within must be numbers',
undefined,
ssfi
);
Expand Down Expand Up @@ -1026,6 +1040,7 @@ module.exports = function (chai, _) {

var target = flag(this, 'object')
var ssfi = flag(this, 'ssfi');
var flagMsg = flag(this, 'message');
var validInstanceOfTarget = constructor === Object(constructor) && (
typeof constructor === 'function' ||
(typeof Symbol !== 'undefined' &&
Expand All @@ -1034,9 +1049,10 @@ module.exports = function (chai, _) {
);

if (!validInstanceOfTarget) {
flagMsg = flagMsg ? flagMsg + ': ' : '';
var constructorType = constructor === null ? 'null' : typeof constructor;
throw new AssertionError(
'The instanceof assertion needs a constructor but ' + constructorType + ' was given.',
flagMsg + 'The instanceof assertion needs a constructor but ' + constructorType + ' was given.',
undefined,
ssfi
);
Expand Down Expand Up @@ -1165,11 +1181,13 @@ module.exports = function (chai, _) {

var isNested = flag(this, 'nested')
, isOwn = flag(this, 'own')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

if (isNested && isOwn) {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'The "nested" and "own" flags cannot be combined.',
flagMsg + 'The "nested" and "own" flags cannot be combined.',
undefined,
ssfi
);
Expand Down Expand Up @@ -1325,8 +1343,9 @@ module.exports = function (chai, _) {
function assertLength (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(obj, msg, ssfi, true).to.have.property('length');
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
var len = obj.length;

this.assert(
Expand Down Expand Up @@ -1385,8 +1404,9 @@ module.exports = function (chai, _) {
Assertion.addMethod('string', function (str, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(obj, msg, ssfi, true).is.a('string');
new Assertion(obj, flagMsg, ssfi, true).is.a('string');

this.assert(
~obj.indexOf(str)
Expand Down Expand Up @@ -1458,7 +1478,10 @@ module.exports = function (chai, _) {
, str
, deepStr = ''
, ok = true
, mixedArgsMsg = 'when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments';
, flagMsg = flag(this, 'message');

flagMsg = flagMsg ? flagMsg + ': ' : '';
var mixedArgsMsg = flagMsg + 'when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments';

if (objType === 'Map' || objType === 'Set') {
deepStr = isDeep ? 'deeply ' : '';
Expand Down Expand Up @@ -1497,7 +1520,7 @@ module.exports = function (chai, _) {
}

if (!keys.length) {
throw new AssertionError('keys required', undefined, ssfi);
throw new AssertionError(flagMsg + 'keys required', undefined, ssfi);
}

var len = keys.length
Expand Down Expand Up @@ -1701,8 +1724,9 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, ssfi = flag(this, 'ssfi')
, flagMsg = flag(this, 'message')
, negate = flag(this, 'negate') || false;
new Assertion(obj, msg, ssfi, true).is.a('function');
new Assertion(obj, flagMsg, ssfi, true).is.a('function');

if (errorLike instanceof RegExp || typeof errorLike === 'string') {
errMsgMatcher = errorLike;
Expand Down Expand Up @@ -1938,12 +1962,14 @@ module.exports = function (chai, _) {
function closeTo(expected, delta, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new Assertion(obj, msg, ssfi, true).is.a('number');
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
if (typeof expected !== 'number' || typeof delta !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
'the arguments to closeTo or approximately must be numbers',
flagMsg + 'the arguments to closeTo or approximately must be numbers',
undefined,
ssfi
);
Expand Down Expand Up @@ -2039,10 +2065,11 @@ module.exports = function (chai, _) {
Assertion.addMethod('members', function (subset, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new Assertion(obj, msg, ssfi, true).to.be.an('array');
new Assertion(subset, msg, ssfi, true).to.be.an('array');
new Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
new Assertion(subset, flagMsg, ssfi, true).to.be.an('array');

var contains = flag(this, 'contains');
var ordered = flag(this, 'ordered');
Expand Down Expand Up @@ -2096,8 +2123,9 @@ module.exports = function (chai, _) {
function oneOf (list, msg) {
if (msg) flag(this, 'message', msg);
var expected = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(list, msg, ssfi, true).to.be.an('array');
new Assertion(list, flagMsg, ssfi, true).to.be.an('array');

this.assert(
list.indexOf(expected) > -1
Expand Down Expand Up @@ -2135,15 +2163,16 @@ module.exports = function (chai, _) {
function assertChanges (target, prop, msg) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(fn, msg, ssfi, true).is.a('function');
new Assertion(fn, flagMsg, ssfi, true).is.a('function');

var initial;
if (!prop) {
new Assertion(target, msg, ssfi, true).is.a('function');
new Assertion(target, flagMsg, ssfi, true).is.a('function');
initial = target();
} else {
new Assertion(target, msg, ssfi, true).to.have.property(prop);
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
initial = target[prop];
}

Expand Down Expand Up @@ -2198,20 +2227,21 @@ module.exports = function (chai, _) {
function assertIncreases (target, prop, msg) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(fn, msg, ssfi, true).is.a('function');
new Assertion(fn, flagMsg, ssfi, true).is.a('function');

var initial;
if (!prop) {
new Assertion(target, msg, ssfi, true).is.a('function');
new Assertion(target, flagMsg, ssfi, true).is.a('function');
initial = target();
} else {
new Assertion(target, msg, ssfi, true).to.have.property(prop);
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
initial = target[prop];
}

// Make sure that the target is a number
new Assertion(initial, msg, ssfi, true).is.a('number');
new Assertion(initial, flagMsg, ssfi, true).is.a('number');

fn();

Expand Down Expand Up @@ -2263,20 +2293,21 @@ module.exports = function (chai, _) {
function assertDecreases (target, prop, msg) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');
new Assertion(fn, msg, ssfi, true).is.a('function');
new Assertion(fn, flagMsg, ssfi, true).is.a('function');

var initial;
if (!prop) {
new Assertion(target, msg, ssfi, true).is.a('function');
new Assertion(target, flagMsg, ssfi, true).is.a('function');
initial = target();
} else {
new Assertion(target, msg, ssfi, true).to.have.property(prop);
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
initial = target[prop];
}

// Make sure that the target is a number
new Assertion(initial, msg, ssfi, true).is.a('number');
new Assertion(initial, flagMsg, ssfi, true).is.a('number');

fn();

Expand Down Expand Up @@ -2315,7 +2346,9 @@ module.exports = function (chai, _) {
* @api public
*/

function assertDelta(delta) {
function assertDelta(delta, msg) {
if (msg) flag(this, 'message', msg);

var msgObj = flag(this, 'deltaMsgObj');
var initial = flag(this, 'initialDeltaValue');
var final = flag(this, 'finalDeltaValue');
Expand Down
3 changes: 2 additions & 1 deletion lib/chai/interface/assert.js
Expand Up @@ -1867,8 +1867,9 @@ module.exports = function (chai, util) {
ok = val !== val2;
break;
default:
msg = msg ? msg + ': ' : msg;
throw new chai.AssertionError(
'Invalid operator "' + operator + '"',
msg + 'Invalid operator "' + operator + '"',
undefined,
assert.operator
);
Expand Down