chai 5.0.0-rc.0 → 5.0.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.
@@ -40,6 +40,8 @@ import * as util from './utils/index.js';
40
40
  * from within another assertion. It's also temporarily set to `true` before
41
41
  * an overwritten assertion gets called by the overwriting assertion.
42
42
  *
43
+ * - `eql`: This flag contains the deepEqual function to be used by the assertion.
44
+ *
43
45
  * @param {Mixed} obj target of the assertion
44
46
  * @param {String} msg (optional) custom error message
45
47
  * @param {Function} ssfi (optional) starting point for removing stack frames
@@ -52,6 +54,7 @@ export function Assertion (obj, msg, ssfi, lockSsfi) {
52
54
  util.flag(this, 'lockSsfi', lockSsfi);
53
55
  util.flag(this, 'object', obj);
54
56
  util.flag(this, 'message', msg);
57
+ util.flag(this, 'eql', config.deepEqual ?? util.eql);
55
58
 
56
59
  return util.proxify(this);
57
60
  }
@@ -90,5 +90,31 @@ export const config = {
90
90
  * @api public
91
91
  */
92
92
 
93
- proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON']
93
+ proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON'],
94
+
95
+ /**
96
+ * ### config.deepEqual
97
+ *
98
+ * User configurable property, defines which a custom function to use for deepEqual
99
+ * comparisons.
100
+ * By default, the function used is the one from the `deep-eql` package without custom comparator.
101
+ *
102
+ * // use a custom comparator
103
+ * chai.config.deepEqual = (expected, actual) => {
104
+ * return chai.util.eql(expected, actual, {
105
+ * comparator: (expected, actual) => {
106
+ * // for non number comparison, use the default behavior
107
+ * if(typeof expected !== 'number') return null;
108
+ * // allow a difference of 10 between compared numbers
109
+ * return typeof actual === 'number' && Math.abs(actual - expected) < 10
110
+ * }
111
+ * })
112
+ * };
113
+ *
114
+ * @param {Function}
115
+ * @api public
116
+ */
117
+
118
+ deepEqual: null
119
+
94
120
  };
@@ -479,7 +479,8 @@ function include (val, msg) {
479
479
  , negate = flag(this, 'negate')
480
480
  , ssfi = flag(this, 'ssfi')
481
481
  , isDeep = flag(this, 'deep')
482
- , descriptor = isDeep ? 'deep ' : '';
482
+ , descriptor = isDeep ? 'deep ' : ''
483
+ , isEql = isDeep ? flag(this, 'eql') : SameValueZero;
483
484
 
484
485
  flagMsg = flagMsg ? flagMsg + ': ' : '';
485
486
 
@@ -503,7 +504,6 @@ function include (val, msg) {
503
504
  break;
504
505
 
505
506
  case 'map':
506
- var isEql = isDeep ? _.eql : SameValueZero;
507
507
  obj.forEach(function (item) {
508
508
  included = included || isEql(item, val);
509
509
  });
@@ -512,7 +512,7 @@ function include (val, msg) {
512
512
  case 'set':
513
513
  if (isDeep) {
514
514
  obj.forEach(function (item) {
515
- included = included || _.eql(item, val);
515
+ included = included || isEql(item, val);
516
516
  });
517
517
  } else {
518
518
  included = obj.has(val);
@@ -522,7 +522,7 @@ function include (val, msg) {
522
522
  case 'array':
523
523
  if (isDeep) {
524
524
  included = obj.some(function (item) {
525
- return _.eql(item, val);
525
+ return isEql(item, val);
526
526
  })
527
527
  } else {
528
528
  included = obj.indexOf(val) !== -1;
@@ -1094,8 +1094,9 @@ Assertion.addMethod('eq', assertEqual);
1094
1094
 
1095
1095
  function assertEql(obj, msg) {
1096
1096
  if (msg) flag(this, 'message', msg);
1097
+ var eql = flag(this, 'eql');
1097
1098
  this.assert(
1098
- _.eql(obj, flag(this, 'object'))
1099
+ eql(obj, flag(this, 'object'))
1099
1100
  , 'expected #{this} to deeply equal #{exp}'
1100
1101
  , 'expected #{this} to not deeply equal #{exp}'
1101
1102
  , obj
@@ -1863,7 +1864,8 @@ function assertProperty (name, val, msg) {
1863
1864
  var isDeep = flag(this, 'deep')
1864
1865
  , negate = flag(this, 'negate')
1865
1866
  , pathInfo = isNested ? _.getPathInfo(obj, name) : null
1866
- , value = isNested ? pathInfo.value : obj[name];
1867
+ , value = isNested ? pathInfo.value : obj[name]
1868
+ , isEql = isDeep ? flag(this, 'eql') : (val1, val2) => val1 === val2;
1867
1869
 
1868
1870
  var descriptor = '';
1869
1871
  if (isDeep) descriptor += 'deep ';
@@ -1890,7 +1892,7 @@ function assertProperty (name, val, msg) {
1890
1892
 
1891
1893
  if (arguments.length > 1) {
1892
1894
  this.assert(
1893
- hasProperty && (isDeep ? _.eql(val, value) : val === value)
1895
+ hasProperty && isEql(val, value)
1894
1896
  , 'expected #{this} to have ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}'
1895
1897
  , 'expected #{this} to not have ' + descriptor + _.inspect(name) + ' of #{act}'
1896
1898
  , val
@@ -2038,9 +2040,10 @@ function assertOwnPropertyDescriptor (name, descriptor, msg) {
2038
2040
  if (msg) flag(this, 'message', msg);
2039
2041
  var obj = flag(this, 'object');
2040
2042
  var actualDescriptor = Object.getOwnPropertyDescriptor(Object(obj), name);
2043
+ var eql = flag(this, 'eql');
2041
2044
  if (actualDescriptor && descriptor) {
2042
2045
  this.assert(
2043
- _.eql(descriptor, actualDescriptor)
2046
+ eql(descriptor, actualDescriptor)
2044
2047
  , 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to match ' + _.inspect(descriptor) + ', got ' + _.inspect(actualDescriptor)
2045
2048
  , 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to not match ' + _.inspect(descriptor)
2046
2049
  , descriptor
@@ -2394,7 +2397,8 @@ function assertKeys (keys) {
2394
2397
  var len = keys.length
2395
2398
  , any = flag(this, 'any')
2396
2399
  , all = flag(this, 'all')
2397
- , expected = keys;
2400
+ , expected = keys
2401
+ , isEql = isDeep ? flag(this, 'eql') : (val1, val2) => val1 === val2;
2398
2402
 
2399
2403
  if (!any && !all) {
2400
2404
  all = true;
@@ -2404,11 +2408,7 @@ function assertKeys (keys) {
2404
2408
  if (any) {
2405
2409
  ok = expected.some(function(expectedKey) {
2406
2410
  return actual.some(function(actualKey) {
2407
- if (isDeep) {
2408
- return _.eql(expectedKey, actualKey);
2409
- } else {
2410
- return expectedKey === actualKey;
2411
- }
2411
+ return isEql(expectedKey, actualKey);
2412
2412
  });
2413
2413
  });
2414
2414
  }
@@ -2417,11 +2417,7 @@ function assertKeys (keys) {
2417
2417
  if (all) {
2418
2418
  ok = expected.every(function(expectedKey) {
2419
2419
  return actual.some(function(actualKey) {
2420
- if (isDeep) {
2421
- return _.eql(expectedKey, actualKey);
2422
- } else {
2423
- return expectedKey === actualKey;
2424
- }
2420
+ return isEql(expectedKey, actualKey);
2425
2421
  });
2426
2422
  });
2427
2423
 
@@ -3109,7 +3105,7 @@ Assertion.addMethod('members', function (subset, msg) {
3109
3105
  failNegateMsg = 'expected #{this} to not have the same ' + subject + ' as #{exp}';
3110
3106
  }
3111
3107
 
3112
- var cmp = flag(this, 'deep') ? _.eql : undefined;
3108
+ var cmp = flag(this, 'deep') ? flag(this, 'eql') : undefined;
3113
3109
 
3114
3110
  this.assert(
3115
3111
  isSubsetOf(subset, obj, cmp, contains, ordered)
@@ -3165,7 +3161,8 @@ function oneOf (list, msg) {
3165
3161
  , flagMsg = flag(this, 'message')
3166
3162
  , ssfi = flag(this, 'ssfi')
3167
3163
  , contains = flag(this, 'contains')
3168
- , isDeep = flag(this, 'deep');
3164
+ , isDeep = flag(this, 'deep')
3165
+ , eql = flag(this, 'eql');
3169
3166
  new Assertion(list, flagMsg, ssfi, true).to.be.an('array');
3170
3167
 
3171
3168
  if (contains) {
@@ -3179,7 +3176,7 @@ function oneOf (list, msg) {
3179
3176
  } else {
3180
3177
  if (isDeep) {
3181
3178
  this.assert(
3182
- list.some(function(possibility) { return _.eql(expected, possibility) })
3179
+ list.some(function(possibility) { return eql(expected, possibility) })
3183
3180
  , 'expected #{this} to deeply equal one of #{exp}'
3184
3181
  , 'expected #{this} to deeply equal one of #{exp}'
3185
3182
  , list
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "Veselin Todorov <hi@vesln.com>",
19
19
  "John Firebaugh <john.firebaugh@gmail.com>"
20
20
  ],
21
- "version": "5.0.0-rc.0",
21
+ "version": "5.0.0",
22
22
  "repository": {
23
23
  "type": "git",
24
24
  "url": "https://github.com/chaijs/chai"