chai 3.2.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -88,8 +88,8 @@ module.exports = function (_chai, util) {
88
88
  *
89
89
  * @name assert
90
90
  * @param {Philosophical} expression to be tested
91
- * @param {String or Function} message or function that returns message to display if expression fails
92
- * @param {String or Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
91
+ * @param {String|Function} message or function that returns message to display if expression fails
92
+ * @param {String|Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
93
93
  * @param {Mixed} expected value (remember to check for negation)
94
94
  * @param {Mixed} actual (optional) will default to `this.obj`
95
95
  * @param {Boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
@@ -35,6 +35,7 @@ module.exports = function (chai, _) {
35
35
  * - same
36
36
  *
37
37
  * @name language chains
38
+ * @namespace BDD
38
39
  * @api public
39
40
  */
40
41
 
@@ -58,6 +59,7 @@ module.exports = function (chai, _) {
58
59
  * .and.not.equal('bar');
59
60
  *
60
61
  * @name not
62
+ * @namespace BDD
61
63
  * @api public
62
64
  */
63
65
 
@@ -82,6 +84,7 @@ module.exports = function (chai, _) {
82
84
  * expect(deepCss).to.have.deep.property('\\.link.\\[target\\]', 42);
83
85
  *
84
86
  * @name deep
87
+ * @namespace BDD
85
88
  * @api public
86
89
  */
87
90
 
@@ -98,6 +101,7 @@ module.exports = function (chai, _) {
98
101
  * expect(foo).to.have.any.keys('bar', 'baz');
99
102
  *
100
103
  * @name any
104
+ * @namespace BDD
101
105
  * @api public
102
106
  */
103
107
 
@@ -116,6 +120,7 @@ module.exports = function (chai, _) {
116
120
  * expect(foo).to.have.all.keys('bar', 'baz');
117
121
  *
118
122
  * @name all
123
+ * @namespace BDD
119
124
  * @api public
120
125
  */
121
126
 
@@ -136,6 +141,7 @@ module.exports = function (chai, _) {
136
141
  * expect({ foo: 'bar' }).to.be.an('object');
137
142
  * expect(null).to.be.a('null');
138
143
  * expect(undefined).to.be.an('undefined');
144
+ * expect(new Error).to.be.an('error');
139
145
  * expect(new Promise).to.be.a('promise');
140
146
  * expect(new Float32Array()).to.be.a('float32array');
141
147
  * expect(Symbol()).to.be.a('symbol');
@@ -150,6 +156,7 @@ module.exports = function (chai, _) {
150
156
  * @alias an
151
157
  * @param {String} type
152
158
  * @param {String} message _optional_
159
+ * @namespace BDD
153
160
  * @api public
154
161
  */
155
162
 
@@ -187,6 +194,7 @@ module.exports = function (chai, _) {
187
194
  * @alias contains
188
195
  * @param {Object|String|Number} obj
189
196
  * @param {String} message _optional_
197
+ * @namespace BDD
190
198
  * @api public
191
199
  */
192
200
 
@@ -195,9 +203,12 @@ module.exports = function (chai, _) {
195
203
  }
196
204
 
197
205
  function include (val, msg) {
206
+ _.expectTypes(this, ['array', 'object', 'string']);
207
+
198
208
  if (msg) flag(this, 'message', msg);
199
209
  var obj = flag(this, 'object');
200
210
  var expected = false;
211
+
201
212
  if (_.type(obj) === 'array' && _.type(val) === 'object') {
202
213
  for (var i in obj) {
203
214
  if (_.eql(obj[i], val)) {
@@ -214,7 +225,7 @@ module.exports = function (chai, _) {
214
225
  for (var k in val) subset[k] = obj[k];
215
226
  expected = _.eql(subset, val);
216
227
  } else {
217
- expected = obj && ~obj.indexOf(val);
228
+ expected = (obj != undefined) && ~obj.indexOf(val);
218
229
  }
219
230
  this.assert(
220
231
  expected
@@ -232,13 +243,14 @@ module.exports = function (chai, _) {
232
243
  *
233
244
  * Asserts that the target is truthy.
234
245
  *
235
- * expect('everthing').to.be.ok;
246
+ * expect('everything').to.be.ok;
236
247
  * expect(1).to.be.ok;
237
248
  * expect(false).to.not.be.ok;
238
249
  * expect(undefined).to.not.be.ok;
239
250
  * expect(null).to.not.be.ok;
240
251
  *
241
252
  * @name ok
253
+ * @namespace BDD
242
254
  * @api public
243
255
  */
244
256
 
@@ -258,6 +270,7 @@ module.exports = function (chai, _) {
258
270
  * expect(1).to.not.be.true;
259
271
  *
260
272
  * @name true
273
+ * @namespace BDD
261
274
  * @api public
262
275
  */
263
276
 
@@ -279,6 +292,7 @@ module.exports = function (chai, _) {
279
292
  * expect(0).to.not.be.false;
280
293
  *
281
294
  * @name false
295
+ * @namespace BDD
282
296
  * @api public
283
297
  */
284
298
 
@@ -300,6 +314,7 @@ module.exports = function (chai, _) {
300
314
  * expect(undefined).to.not.be.null;
301
315
  *
302
316
  * @name null
317
+ * @namespace BDD
303
318
  * @api public
304
319
  */
305
320
 
@@ -320,6 +335,7 @@ module.exports = function (chai, _) {
320
335
  * expect(null).to.not.be.undefined;
321
336
  *
322
337
  * @name undefined
338
+ * @namespace BDD
323
339
  * @api public
324
340
  */
325
341
 
@@ -339,6 +355,7 @@ module.exports = function (chai, _) {
339
355
  * expect(4).not.to.be.NaN;
340
356
  *
341
357
  * @name NaN
358
+ * @namespace BDD
342
359
  * @api public
343
360
  */
344
361
 
@@ -364,6 +381,7 @@ module.exports = function (chai, _) {
364
381
  * expect(baz).to.not.exist;
365
382
  *
366
383
  * @name exist
384
+ * @namespace BDD
367
385
  * @api public
368
386
  */
369
387
 
@@ -388,6 +406,7 @@ module.exports = function (chai, _) {
388
406
  * expect({}).to.be.empty;
389
407
  *
390
408
  * @name empty
409
+ * @namespace BDD
391
410
  * @api public
392
411
  */
393
412
 
@@ -419,6 +438,7 @@ module.exports = function (chai, _) {
419
438
  *
420
439
  * @name arguments
421
440
  * @alias Arguments
441
+ * @namespace BDD
422
442
  * @api public
423
443
  */
424
444
 
@@ -454,6 +474,7 @@ module.exports = function (chai, _) {
454
474
  * @alias deep.equal
455
475
  * @param {Mixed} value
456
476
  * @param {String} message _optional_
477
+ * @namespace BDD
457
478
  * @api public
458
479
  */
459
480
 
@@ -490,6 +511,7 @@ module.exports = function (chai, _) {
490
511
  * @alias eqls
491
512
  * @param {Mixed} value
492
513
  * @param {String} message _optional_
514
+ * @namespace BDD
493
515
  * @api public
494
516
  */
495
517
 
@@ -528,6 +550,7 @@ module.exports = function (chai, _) {
528
550
  * @alias greaterThan
529
551
  * @param {Number} value
530
552
  * @param {String} message _optional_
553
+ * @namespace BDD
531
554
  * @api public
532
555
  */
533
556
 
@@ -576,6 +599,7 @@ module.exports = function (chai, _) {
576
599
  * @alias gte
577
600
  * @param {Number} value
578
601
  * @param {String} message _optional_
602
+ * @namespace BDD
579
603
  * @api public
580
604
  */
581
605
 
@@ -624,6 +648,7 @@ module.exports = function (chai, _) {
624
648
  * @alias lessThan
625
649
  * @param {Number} value
626
650
  * @param {String} message _optional_
651
+ * @namespace BDD
627
652
  * @api public
628
653
  */
629
654
 
@@ -672,6 +697,7 @@ module.exports = function (chai, _) {
672
697
  * @alias lte
673
698
  * @param {Number} value
674
699
  * @param {String} message _optional_
700
+ * @namespace BDD
675
701
  * @api public
676
702
  */
677
703
 
@@ -719,6 +745,7 @@ module.exports = function (chai, _) {
719
745
  * @param {Number} start lowerbound inclusive
720
746
  * @param {Number} finish upperbound inclusive
721
747
  * @param {String} message _optional_
748
+ * @namespace BDD
722
749
  * @api public
723
750
  */
724
751
 
@@ -758,6 +785,7 @@ module.exports = function (chai, _) {
758
785
  * @param {Constructor} constructor
759
786
  * @param {String} message _optional_
760
787
  * @alias instanceOf
788
+ * @namespace BDD
761
789
  * @api public
762
790
  */
763
791
 
@@ -842,6 +870,7 @@ module.exports = function (chai, _) {
842
870
  * @param {Mixed} value (optional)
843
871
  * @param {String} message _optional_
844
872
  * @returns value of property for chaining
873
+ * @namespace BDD
845
874
  * @api public
846
875
  */
847
876
 
@@ -897,6 +926,7 @@ module.exports = function (chai, _) {
897
926
  * @alias haveOwnProperty
898
927
  * @param {String} name
899
928
  * @param {String} message _optional_
929
+ * @namespace BDD
900
930
  * @api public
901
931
  */
902
932
 
@@ -929,6 +959,7 @@ module.exports = function (chai, _) {
929
959
  * @param {String} name
930
960
  * @param {Object} descriptor _optional_
931
961
  * @param {String} message _optional_
962
+ * @namespace BDD
932
963
  * @api public
933
964
  */
934
965
 
@@ -981,6 +1012,7 @@ module.exports = function (chai, _) {
981
1012
  * switched to use `lengthOf(value)` instead.
982
1013
  *
983
1014
  * @name length
1015
+ * @namespace BDD
984
1016
  * @api public
985
1017
  */
986
1018
 
@@ -996,6 +1028,7 @@ module.exports = function (chai, _) {
996
1028
  * @name lengthOf
997
1029
  * @param {Number} length
998
1030
  * @param {String} message _optional_
1031
+ * @namespace BDD
999
1032
  * @api public
1000
1033
  */
1001
1034
 
@@ -1032,6 +1065,7 @@ module.exports = function (chai, _) {
1032
1065
  * @alias matches
1033
1066
  * @param {RegExp} RegularExpression
1034
1067
  * @param {String} message _optional_
1068
+ * @namespace BDD
1035
1069
  * @api public
1036
1070
  */
1037
1071
  function assertMatch(re, msg) {
@@ -1057,6 +1091,7 @@ module.exports = function (chai, _) {
1057
1091
  * @name string
1058
1092
  * @param {String} string
1059
1093
  * @param {String} message _optional_
1094
+ * @namespace BDD
1060
1095
  * @api public
1061
1096
  */
1062
1097
 
@@ -1107,7 +1142,8 @@ module.exports = function (chai, _) {
1107
1142
  *
1108
1143
  * @name keys
1109
1144
  * @alias key
1110
- * @param {String...|Array|Object} keys
1145
+ * @param {...String|Array|Object} keys
1146
+ * @namespace BDD
1111
1147
  * @api public
1112
1148
  */
1113
1149
 
@@ -1210,7 +1246,6 @@ module.exports = function (chai, _) {
1210
1246
  * expect(fn).to.not.throw('good function');
1211
1247
  * expect(fn).to.throw(ReferenceError, /bad function/);
1212
1248
  * expect(fn).to.throw(err);
1213
- * expect(fn).to.not.throw(new RangeError('Out of range.'));
1214
1249
  *
1215
1250
  * Please note that when a throw expectation is negated, it will check each
1216
1251
  * parameter independently, starting with error constructor type. The appropriate way
@@ -1228,6 +1263,7 @@ module.exports = function (chai, _) {
1228
1263
  * @param {String} message _optional_
1229
1264
  * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
1230
1265
  * @returns error for chaining (null if no error)
1266
+ * @namespace BDD
1231
1267
  * @api public
1232
1268
  */
1233
1269
 
@@ -1252,9 +1288,9 @@ module.exports = function (chai, _) {
1252
1288
  constructor = null;
1253
1289
  errMsg = null;
1254
1290
  } else if (typeof constructor === 'function') {
1255
- name = constructor.prototype.name || constructor.name;
1256
- if (name === 'Error' && constructor !== Error) {
1257
- name = (new constructor()).name;
1291
+ name = constructor.prototype.name;
1292
+ if (!name || (name === 'Error' && constructor !== Error)) {
1293
+ name = constructor.name || (new constructor()).name;
1258
1294
  }
1259
1295
  } else {
1260
1296
  constructor = null;
@@ -1371,6 +1407,7 @@ module.exports = function (chai, _) {
1371
1407
  * @alias respondsTo
1372
1408
  * @param {String} method
1373
1409
  * @param {String} message _optional_
1410
+ * @namespace BDD
1374
1411
  * @api public
1375
1412
  */
1376
1413
 
@@ -1405,6 +1442,7 @@ module.exports = function (chai, _) {
1405
1442
  * expect(Foo).itself.not.to.respondTo('baz');
1406
1443
  *
1407
1444
  * @name itself
1445
+ * @namespace BDD
1408
1446
  * @api public
1409
1447
  */
1410
1448
 
@@ -1423,6 +1461,7 @@ module.exports = function (chai, _) {
1423
1461
  * @alias satisfies
1424
1462
  * @param {Function} matcher
1425
1463
  * @param {String} message _optional_
1464
+ * @namespace BDD
1426
1465
  * @api public
1427
1466
  */
1428
1467
 
@@ -1438,7 +1477,7 @@ module.exports = function (chai, _) {
1438
1477
  , result
1439
1478
  );
1440
1479
  }
1441
-
1480
+
1442
1481
  Assertion.addMethod('satisfy', satisfy);
1443
1482
  Assertion.addMethod('satisfies', satisfy);
1444
1483
 
@@ -1450,19 +1489,21 @@ module.exports = function (chai, _) {
1450
1489
  * expect(1.5).to.be.closeTo(1, 0.5);
1451
1490
  *
1452
1491
  * @name closeTo
1492
+ * @alias approximately
1453
1493
  * @param {Number} expected
1454
1494
  * @param {Number} delta
1455
1495
  * @param {String} message _optional_
1496
+ * @namespace BDD
1456
1497
  * @api public
1457
1498
  */
1458
1499
 
1459
- Assertion.addMethod('closeTo', function (expected, delta, msg) {
1500
+ function closeTo(expected, delta, msg) {
1460
1501
  if (msg) flag(this, 'message', msg);
1461
1502
  var obj = flag(this, 'object');
1462
1503
 
1463
1504
  new Assertion(obj, msg).is.a('number');
1464
1505
  if (_.type(expected) !== 'number' || _.type(delta) !== 'number') {
1465
- throw new Error('the arguments to closeTo must be numbers');
1506
+ throw new Error('the arguments to closeTo or approximately must be numbers');
1466
1507
  }
1467
1508
 
1468
1509
  this.assert(
@@ -1470,7 +1511,10 @@ module.exports = function (chai, _) {
1470
1511
  , 'expected #{this} to be close to ' + expected + ' +/- ' + delta
1471
1512
  , 'expected #{this} not to be close to ' + expected + ' +/- ' + delta
1472
1513
  );
1473
- });
1514
+ }
1515
+
1516
+ Assertion.addMethod('closeTo', closeTo);
1517
+ Assertion.addMethod('approximately', closeTo);
1474
1518
 
1475
1519
  function isSubsetOf(subset, superset, cmp) {
1476
1520
  return subset.every(function(elem) {
@@ -1501,6 +1545,7 @@ module.exports = function (chai, _) {
1501
1545
  * @name members
1502
1546
  * @param {Array} set
1503
1547
  * @param {String} message _optional_
1548
+ * @namespace BDD
1504
1549
  * @api public
1505
1550
  */
1506
1551
 
@@ -1532,6 +1577,45 @@ module.exports = function (chai, _) {
1532
1577
  );
1533
1578
  });
1534
1579
 
1580
+ /**
1581
+ * ### .oneOf(list)
1582
+ *
1583
+ * Assert that a value appears somewhere in the top level of array `list`.
1584
+ *
1585
+ * expect('a').to.be.oneOf(['a', 'b', 'c']);
1586
+ * expect(9).to.not.be.oneOf(['z']);
1587
+ * expect([3]).to.not.be.oneOf([1, 2, [3]]);
1588
+ *
1589
+ * var three = [3];
1590
+ * // for object-types, contents are not compared
1591
+ * expect(three).to.not.be.oneOf([1, 2, [3]]);
1592
+ * // comparing references works
1593
+ * expect(three).to.be.oneOf([1, 2, three]);
1594
+ *
1595
+ * @name oneOf
1596
+ * @param {Array<*>} list
1597
+ * @param {String} message _optional_
1598
+ * @namespace BDD
1599
+ * @api public
1600
+ */
1601
+
1602
+ function oneOf (list, msg) {
1603
+ if (msg) flag(this, 'message', msg);
1604
+ var expected = flag(this, 'object');
1605
+ new Assertion(list).to.be.an('array');
1606
+
1607
+ this.assert(
1608
+ list.indexOf(expected) > -1
1609
+ , 'expected #{this} to be one of #{exp}'
1610
+ , 'expected #{this} to not be one of #{exp}'
1611
+ , list
1612
+ , expected
1613
+ );
1614
+ }
1615
+
1616
+ Assertion.addMethod('oneOf', oneOf);
1617
+
1618
+
1535
1619
  /**
1536
1620
  * ### .change(function)
1537
1621
  *
@@ -1541,7 +1625,7 @@ module.exports = function (chai, _) {
1541
1625
  * var fn = function() { obj.val += 3 };
1542
1626
  * var noChangeFn = function() { return 'foo' + 'bar'; }
1543
1627
  * expect(fn).to.change(obj, 'val');
1544
- * expect(noChangFn).to.not.change(obj, 'val')
1628
+ * expect(noChangeFn).to.not.change(obj, 'val')
1545
1629
  *
1546
1630
  * @name change
1547
1631
  * @alias changes
@@ -1549,6 +1633,7 @@ module.exports = function (chai, _) {
1549
1633
  * @param {String} object
1550
1634
  * @param {String} property name
1551
1635
  * @param {String} message _optional_
1636
+ * @namespace BDD
1552
1637
  * @api public
1553
1638
  */
1554
1639
 
@@ -1586,6 +1671,7 @@ module.exports = function (chai, _) {
1586
1671
  * @param {String} object
1587
1672
  * @param {String} property name
1588
1673
  * @param {String} message _optional_
1674
+ * @namespace BDD
1589
1675
  * @api public
1590
1676
  */
1591
1677
 
@@ -1623,6 +1709,7 @@ module.exports = function (chai, _) {
1623
1709
  * @param {String} object
1624
1710
  * @param {String} property name
1625
1711
  * @param {String} message _optional_
1712
+ * @namespace BDD
1626
1713
  * @api public
1627
1714
  */
1628
1715
 
@@ -1648,7 +1735,7 @@ module.exports = function (chai, _) {
1648
1735
  /**
1649
1736
  * ### .extensible
1650
1737
  *
1651
- * Asserts that the target is extensible (can have new properties added to
1738
+ * Asserts that the target is extensible (can have new properties added to
1652
1739
  * it).
1653
1740
  *
1654
1741
  * var nonExtensibleObject = Object.preventExtensions({});
@@ -1661,14 +1748,29 @@ module.exports = function (chai, _) {
1661
1748
  * expect(frozenObject).to.not.be.extensible;
1662
1749
  *
1663
1750
  * @name extensible
1751
+ * @namespace BDD
1664
1752
  * @api public
1665
1753
  */
1666
1754
 
1667
1755
  Assertion.addProperty('extensible', function() {
1668
1756
  var obj = flag(this, 'object');
1669
1757
 
1758
+ // In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError.
1759
+ // In ES6, a non-object argument will be treated as if it was a non-extensible ordinary object, simply return false.
1760
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible
1761
+ // The following provides ES6 behavior when a TypeError is thrown under ES5.
1762
+
1763
+ var isExtensible;
1764
+
1765
+ try {
1766
+ isExtensible = Object.isExtensible(obj);
1767
+ } catch (err) {
1768
+ if (err instanceof TypeError) isExtensible = false;
1769
+ else throw err;
1770
+ }
1771
+
1670
1772
  this.assert(
1671
- Object.isExtensible(obj)
1773
+ isExtensible
1672
1774
  , 'expected #{this} to be extensible'
1673
1775
  , 'expected #{this} to not be extensible'
1674
1776
  );
@@ -1688,14 +1790,29 @@ module.exports = function (chai, _) {
1688
1790
  * expect({}).to.not.be.sealed;
1689
1791
  *
1690
1792
  * @name sealed
1793
+ * @namespace BDD
1691
1794
  * @api public
1692
1795
  */
1693
1796
 
1694
1797
  Assertion.addProperty('sealed', function() {
1695
1798
  var obj = flag(this, 'object');
1696
1799
 
1800
+ // In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError.
1801
+ // In ES6, a non-object argument will be treated as if it was a sealed ordinary object, simply return true.
1802
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed
1803
+ // The following provides ES6 behavior when a TypeError is thrown under ES5.
1804
+
1805
+ var isSealed;
1806
+
1807
+ try {
1808
+ isSealed = Object.isSealed(obj);
1809
+ } catch (err) {
1810
+ if (err instanceof TypeError) isSealed = true;
1811
+ else throw err;
1812
+ }
1813
+
1697
1814
  this.assert(
1698
- Object.isSealed(obj)
1815
+ isSealed
1699
1816
  , 'expected #{this} to be sealed'
1700
1817
  , 'expected #{this} to not be sealed'
1701
1818
  );
@@ -1713,17 +1830,31 @@ module.exports = function (chai, _) {
1713
1830
  * expect({}).to.not.be.frozen;
1714
1831
  *
1715
1832
  * @name frozen
1833
+ * @namespace BDD
1716
1834
  * @api public
1717
1835
  */
1718
1836
 
1719
1837
  Assertion.addProperty('frozen', function() {
1720
1838
  var obj = flag(this, 'object');
1721
1839
 
1840
+ // In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError.
1841
+ // In ES6, a non-object argument will be treated as if it was a frozen ordinary object, simply return true.
1842
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen
1843
+ // The following provides ES6 behavior when a TypeError is thrown under ES5.
1844
+
1845
+ var isFrozen;
1846
+
1847
+ try {
1848
+ isFrozen = Object.isFrozen(obj);
1849
+ } catch (err) {
1850
+ if (err instanceof TypeError) isFrozen = true;
1851
+ else throw err;
1852
+ }
1853
+
1722
1854
  this.assert(
1723
- Object.isFrozen(obj)
1855
+ isFrozen
1724
1856
  , 'expected #{this} to be frozen'
1725
1857
  , 'expected #{this} to not be frozen'
1726
1858
  );
1727
1859
  });
1728
-
1729
1860
  };