Skip to content

Commit a515ece

Browse files
authored
Merge pull request #947 from meeber/fix-messages
fix: always honor custom message
2 parents e5ac3b0 + 7f8a268 commit a515ece

File tree

6 files changed

+1268
-554
lines changed

6 files changed

+1268
-554
lines changed

lib/chai/core/assertions.js

+76-43
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ module.exports = function (chai, _) {
299299
}
300300

301301
function include (val, msg) {
302+
if (msg) flag(this, 'message', msg);
303+
302304
_.expectTypes(this, ['array', 'object', 'string'], flag(this, 'ssfi'));
303305

304-
if (msg) flag(this, 'message', msg);
305306
var obj = flag(this, 'object')
306307
, objType = _.type(obj).toLowerCase()
307308
, isDeep = flag(this, 'deep')
@@ -535,8 +536,11 @@ module.exports = function (chai, _) {
535536
Assertion.addProperty('empty', function () {
536537
var val = flag(this, 'object')
537538
, ssfi = flag(this, 'ssfi')
539+
, flagMsg = flag(this, 'message')
538540
, itemsCount;
539541

542+
flagMsg = flagMsg ? flagMsg + ': ' : '';
543+
540544
switch (_.type(val).toLowerCase()) {
541545
case 'array':
542546
case 'string':
@@ -549,17 +553,17 @@ module.exports = function (chai, _) {
549553
case 'weakmap':
550554
case 'weakset':
551555
throw new AssertionError(
552-
'.empty was passed a weak collection',
556+
flagMsg + '.empty was passed a weak collection',
553557
undefined,
554558
ssfi
555559
);
556560
case 'function':
557-
var msg = '.empty was passed a function ' + _.getName(val);
561+
var msg = flagMsg + '.empty was passed a function ' + _.getName(val);
558562
throw new AssertionError(msg.trim(), undefined, ssfi);
559563
default:
560564
if (val !== Object(val)) {
561565
throw new AssertionError(
562-
'.empty was passed non-string primitive ' + _.inspect(val),
566+
flagMsg + '.empty was passed non-string primitive ' + _.inspect(val),
563567
undefined,
564568
ssfi
565569
);
@@ -706,17 +710,19 @@ module.exports = function (chai, _) {
706710
if (msg) flag(this, 'message', msg);
707711
var obj = flag(this, 'object')
708712
, doLength = flag(this, 'doLength')
713+
, flagMsg = flag(this, 'message')
709714
, ssfi = flag(this, 'ssfi');
710715

711716
if (doLength) {
712-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
717+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
713718
} else {
714-
new Assertion(obj, msg, ssfi, true).is.a('number');
719+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
715720
}
716721

717722
if (typeof n !== 'number') {
723+
flagMsg = flagMsg ? flagMsg + ': ' : '';
718724
throw new AssertionError(
719-
'the argument to above must be a number',
725+
flagMsg + 'the argument to above must be a number',
720726
undefined,
721727
ssfi
722728
);
@@ -772,17 +778,19 @@ module.exports = function (chai, _) {
772778
if (msg) flag(this, 'message', msg);
773779
var obj = flag(this, 'object')
774780
, doLength = flag(this, 'doLength')
781+
, flagMsg = flag(this, 'message')
775782
, ssfi = flag(this, 'ssfi');
776783

777784
if (doLength) {
778-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
785+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
779786
} else {
780-
new Assertion(obj, msg, ssfi, true).is.a('number');
787+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
781788
}
782789

783790
if (typeof n !== 'number') {
791+
flagMsg = flagMsg ? flagMsg + ': ' : '';
784792
throw new AssertionError(
785-
'the argument to least must be a number',
793+
flagMsg + 'the argument to least must be a number',
786794
undefined,
787795
ssfi
788796
);
@@ -838,17 +846,19 @@ module.exports = function (chai, _) {
838846
if (msg) flag(this, 'message', msg);
839847
var obj = flag(this, 'object')
840848
, doLength = flag(this, 'doLength')
849+
, flagMsg = flag(this, 'message')
841850
, ssfi = flag(this, 'ssfi');
842851

843852
if (doLength) {
844-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
853+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
845854
} else {
846-
new Assertion(obj, msg, ssfi, true).is.a('number');
855+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
847856
}
848857

849858
if (typeof n !== 'number') {
859+
flagMsg = flagMsg ? flagMsg + ': ' : '';
850860
throw new AssertionError(
851-
'the argument to below must be a number',
861+
flagMsg + 'the argument to below must be a number',
852862
undefined,
853863
ssfi
854864
);
@@ -904,17 +914,19 @@ module.exports = function (chai, _) {
904914
if (msg) flag(this, 'message', msg);
905915
var obj = flag(this, 'object')
906916
, doLength = flag(this, 'doLength')
917+
, flagMsg = flag(this, 'message')
907918
, ssfi = flag(this, 'ssfi');
908919

909920
if (doLength) {
910-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
921+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
911922
} else {
912-
new Assertion(obj, msg, ssfi, true).is.a('number');
923+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
913924
}
914925

915926
if (typeof n !== 'number') {
927+
flagMsg = flagMsg ? flagMsg + ': ' : '';
916928
throw new AssertionError(
917-
'the argument to most must be a number',
929+
flagMsg + 'the argument to most must be a number',
918930
undefined,
919931
ssfi
920932
);
@@ -970,17 +982,19 @@ module.exports = function (chai, _) {
970982
var obj = flag(this, 'object')
971983
, range = start + '..' + finish
972984
, doLength = flag(this, 'doLength')
985+
, flagMsg = flag(this, 'message')
973986
, ssfi = flag(this, 'ssfi');
974987

975988
if (doLength) {
976-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
989+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
977990
} else {
978-
new Assertion(obj, msg, ssfi, true).is.a('number');
991+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
979992
}
980993

981994
if (typeof start !== 'number' || typeof finish !== 'number') {
995+
flagMsg = flagMsg ? flagMsg + ': ' : '';
982996
throw new AssertionError(
983-
'the arguments to within must be numbers',
997+
flagMsg + 'the arguments to within must be numbers',
984998
undefined,
985999
ssfi
9861000
);
@@ -1026,6 +1040,7 @@ module.exports = function (chai, _) {
10261040

10271041
var target = flag(this, 'object')
10281042
var ssfi = flag(this, 'ssfi');
1043+
var flagMsg = flag(this, 'message');
10291044
var validInstanceOfTarget = constructor === Object(constructor) && (
10301045
typeof constructor === 'function' ||
10311046
(typeof Symbol !== 'undefined' &&
@@ -1034,9 +1049,10 @@ module.exports = function (chai, _) {
10341049
);
10351050

10361051
if (!validInstanceOfTarget) {
1052+
flagMsg = flagMsg ? flagMsg + ': ' : '';
10371053
var constructorType = constructor === null ? 'null' : typeof constructor;
10381054
throw new AssertionError(
1039-
'The instanceof assertion needs a constructor but ' + constructorType + ' was given.',
1055+
flagMsg + 'The instanceof assertion needs a constructor but ' + constructorType + ' was given.',
10401056
undefined,
10411057
ssfi
10421058
);
@@ -1165,11 +1181,13 @@ module.exports = function (chai, _) {
11651181

11661182
var isNested = flag(this, 'nested')
11671183
, isOwn = flag(this, 'own')
1184+
, flagMsg = flag(this, 'message')
11681185
, ssfi = flag(this, 'ssfi');
11691186

11701187
if (isNested && isOwn) {
1188+
flagMsg = flagMsg ? flagMsg + ': ' : '';
11711189
throw new AssertionError(
1172-
'The "nested" and "own" flags cannot be combined.',
1190+
flagMsg + 'The "nested" and "own" flags cannot be combined.',
11731191
undefined,
11741192
ssfi
11751193
);
@@ -1325,8 +1343,9 @@ module.exports = function (chai, _) {
13251343
function assertLength (n, msg) {
13261344
if (msg) flag(this, 'message', msg);
13271345
var obj = flag(this, 'object')
1346+
, flagMsg = flag(this, 'message')
13281347
, ssfi = flag(this, 'ssfi');
1329-
new Assertion(obj, msg, ssfi, true).to.have.property('length');
1348+
new Assertion(obj, flagMsg, ssfi, true).to.have.property('length');
13301349
var len = obj.length;
13311350

13321351
this.assert(
@@ -1385,8 +1404,9 @@ module.exports = function (chai, _) {
13851404
Assertion.addMethod('string', function (str, msg) {
13861405
if (msg) flag(this, 'message', msg);
13871406
var obj = flag(this, 'object')
1407+
, flagMsg = flag(this, 'message')
13881408
, ssfi = flag(this, 'ssfi');
1389-
new Assertion(obj, msg, ssfi, true).is.a('string');
1409+
new Assertion(obj, flagMsg, ssfi, true).is.a('string');
13901410

13911411
this.assert(
13921412
~obj.indexOf(str)
@@ -1458,7 +1478,10 @@ module.exports = function (chai, _) {
14581478
, str
14591479
, deepStr = ''
14601480
, ok = true
1461-
, mixedArgsMsg = 'when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments';
1481+
, flagMsg = flag(this, 'message');
1482+
1483+
flagMsg = flagMsg ? flagMsg + ': ' : '';
1484+
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';
14621485

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

14991522
if (!keys.length) {
1500-
throw new AssertionError('keys required', undefined, ssfi);
1523+
throw new AssertionError(flagMsg + 'keys required', undefined, ssfi);
15011524
}
15021525

15031526
var len = keys.length
@@ -1701,8 +1724,9 @@ module.exports = function (chai, _) {
17011724
if (msg) flag(this, 'message', msg);
17021725
var obj = flag(this, 'object')
17031726
, ssfi = flag(this, 'ssfi')
1727+
, flagMsg = flag(this, 'message')
17041728
, negate = flag(this, 'negate') || false;
1705-
new Assertion(obj, msg, ssfi, true).is.a('function');
1729+
new Assertion(obj, flagMsg, ssfi, true).is.a('function');
17061730

17071731
if (errorLike instanceof RegExp || typeof errorLike === 'string') {
17081732
errMsgMatcher = errorLike;
@@ -1938,12 +1962,14 @@ module.exports = function (chai, _) {
19381962
function closeTo(expected, delta, msg) {
19391963
if (msg) flag(this, 'message', msg);
19401964
var obj = flag(this, 'object')
1965+
, flagMsg = flag(this, 'message')
19411966
, ssfi = flag(this, 'ssfi');
19421967

1943-
new Assertion(obj, msg, ssfi, true).is.a('number');
1968+
new Assertion(obj, flagMsg, ssfi, true).is.a('number');
19441969
if (typeof expected !== 'number' || typeof delta !== 'number') {
1970+
flagMsg = flagMsg ? flagMsg + ': ' : '';
19451971
throw new AssertionError(
1946-
'the arguments to closeTo or approximately must be numbers',
1972+
flagMsg + 'the arguments to closeTo or approximately must be numbers',
19471973
undefined,
19481974
ssfi
19491975
);
@@ -2039,10 +2065,11 @@ module.exports = function (chai, _) {
20392065
Assertion.addMethod('members', function (subset, msg) {
20402066
if (msg) flag(this, 'message', msg);
20412067
var obj = flag(this, 'object')
2068+
, flagMsg = flag(this, 'message')
20422069
, ssfi = flag(this, 'ssfi');
20432070

2044-
new Assertion(obj, msg, ssfi, true).to.be.an('array');
2045-
new Assertion(subset, msg, ssfi, true).to.be.an('array');
2071+
new Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
2072+
new Assertion(subset, flagMsg, ssfi, true).to.be.an('array');
20462073

20472074
var contains = flag(this, 'contains');
20482075
var ordered = flag(this, 'ordered');
@@ -2096,8 +2123,9 @@ module.exports = function (chai, _) {
20962123
function oneOf (list, msg) {
20972124
if (msg) flag(this, 'message', msg);
20982125
var expected = flag(this, 'object')
2126+
, flagMsg = flag(this, 'message')
20992127
, ssfi = flag(this, 'ssfi');
2100-
new Assertion(list, msg, ssfi, true).to.be.an('array');
2128+
new Assertion(list, flagMsg, ssfi, true).to.be.an('array');
21012129

21022130
this.assert(
21032131
list.indexOf(expected) > -1
@@ -2135,15 +2163,16 @@ module.exports = function (chai, _) {
21352163
function assertChanges (target, prop, msg) {
21362164
if (msg) flag(this, 'message', msg);
21372165
var fn = flag(this, 'object')
2166+
, flagMsg = flag(this, 'message')
21382167
, ssfi = flag(this, 'ssfi');
2139-
new Assertion(fn, msg, ssfi, true).is.a('function');
2168+
new Assertion(fn, flagMsg, ssfi, true).is.a('function');
21402169

21412170
var initial;
21422171
if (!prop) {
2143-
new Assertion(target, msg, ssfi, true).is.a('function');
2172+
new Assertion(target, flagMsg, ssfi, true).is.a('function');
21442173
initial = target();
21452174
} else {
2146-
new Assertion(target, msg, ssfi, true).to.have.property(prop);
2175+
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
21472176
initial = target[prop];
21482177
}
21492178

@@ -2198,20 +2227,21 @@ module.exports = function (chai, _) {
21982227
function assertIncreases (target, prop, msg) {
21992228
if (msg) flag(this, 'message', msg);
22002229
var fn = flag(this, 'object')
2230+
, flagMsg = flag(this, 'message')
22012231
, ssfi = flag(this, 'ssfi');
2202-
new Assertion(fn, msg, ssfi, true).is.a('function');
2232+
new Assertion(fn, flagMsg, ssfi, true).is.a('function');
22032233

22042234
var initial;
22052235
if (!prop) {
2206-
new Assertion(target, msg, ssfi, true).is.a('function');
2236+
new Assertion(target, flagMsg, ssfi, true).is.a('function');
22072237
initial = target();
22082238
} else {
2209-
new Assertion(target, msg, ssfi, true).to.have.property(prop);
2239+
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
22102240
initial = target[prop];
22112241
}
22122242

22132243
// Make sure that the target is a number
2214-
new Assertion(initial, msg, ssfi, true).is.a('number');
2244+
new Assertion(initial, flagMsg, ssfi, true).is.a('number');
22152245

22162246
fn();
22172247

@@ -2263,20 +2293,21 @@ module.exports = function (chai, _) {
22632293
function assertDecreases (target, prop, msg) {
22642294
if (msg) flag(this, 'message', msg);
22652295
var fn = flag(this, 'object')
2296+
, flagMsg = flag(this, 'message')
22662297
, ssfi = flag(this, 'ssfi');
2267-
new Assertion(fn, msg, ssfi, true).is.a('function');
2298+
new Assertion(fn, flagMsg, ssfi, true).is.a('function');
22682299

22692300
var initial;
22702301
if (!prop) {
2271-
new Assertion(target, msg, ssfi, true).is.a('function');
2302+
new Assertion(target, flagMsg, ssfi, true).is.a('function');
22722303
initial = target();
22732304
} else {
2274-
new Assertion(target, msg, ssfi, true).to.have.property(prop);
2305+
new Assertion(target, flagMsg, ssfi, true).to.have.property(prop);
22752306
initial = target[prop];
22762307
}
22772308

22782309
// Make sure that the target is a number
2279-
new Assertion(initial, msg, ssfi, true).is.a('number');
2310+
new Assertion(initial, flagMsg, ssfi, true).is.a('number');
22802311

22812312
fn();
22822313

@@ -2315,7 +2346,9 @@ module.exports = function (chai, _) {
23152346
* @api public
23162347
*/
23172348

2318-
function assertDelta(delta) {
2349+
function assertDelta(delta, msg) {
2350+
if (msg) flag(this, 'message', msg);
2351+
23192352
var msgObj = flag(this, 'deltaMsgObj');
23202353
var initial = flag(this, 'initialDeltaValue');
23212354
var final = flag(this, 'finalDeltaValue');

lib/chai/interface/assert.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,9 @@ module.exports = function (chai, util) {
18671867
ok = val !== val2;
18681868
break;
18691869
default:
1870+
msg = msg ? msg + ': ' : msg;
18701871
throw new chai.AssertionError(
1871-
'Invalid operator "' + operator + '"',
1872+
msg + 'Invalid operator "' + operator + '"',
18721873
undefined,
18731874
assert.operator
18741875
);

0 commit comments

Comments
 (0)