@@ -150,6 +150,8 @@ module.exports = function (_chai, util) {
150
150
* from within another assertion. It's also temporarily set to `true` before
151
151
* an overwritten assertion gets called by the overwriting assertion.
152
152
*
153
+ * - `eql`: This flag contains the deepEqual function to be used by the assertion.
154
+ *
153
155
* @param {Mixed } obj target of the assertion
154
156
* @param {String } msg (optional) custom error message
155
157
* @param {Function } ssfi (optional) starting point for removing stack frames
@@ -162,6 +164,7 @@ module.exports = function (_chai, util) {
162
164
flag ( this , 'lockSsfi' , lockSsfi ) ;
163
165
flag ( this , 'object' , obj ) ;
164
166
flag ( this , 'message' , msg ) ;
167
+ flag ( this , 'eql' , config . deepEqual ?? util . eql ) ;
165
168
166
169
return util . proxify ( this ) ;
167
170
}
@@ -365,7 +368,33 @@ module.exports = {
365
368
* @api public
366
369
*/
367
370
368
- proxyExcludedKeys : [ 'then' , 'catch' , 'inspect' , 'toJSON' ]
371
+ proxyExcludedKeys : [ 'then' , 'catch' , 'inspect' , 'toJSON' ] ,
372
+
373
+ /**
374
+ * ### config.deepEqual
375
+ *
376
+ * User configurable property, defines which a custom function to use for deepEqual
377
+ * comparisons.
378
+ * By default, the function used is the one from the `deep-eql` package without custom comparator.
379
+ *
380
+ * // use a custom comparator
381
+ * chai.config.deepEqual = (expected, actual) => {
382
+ * return chai.util.eql(expected, actual, {
383
+ * comparator: (expected, actual) => {
384
+ * // for non number comparison, use the default behavior
385
+ * if(typeof expected !== 'number') return null;
386
+ * // allow a difference of 10 between compared numbers
387
+ * return typeof actual === 'number' && Math.abs(actual - expected) < 10
388
+ * }
389
+ * })
390
+ * };
391
+ *
392
+ * @param {Function }
393
+ * @api public
394
+ */
395
+
396
+ deepEqual : null
397
+
369
398
} ;
370
399
371
400
} , { } ] , 5 :[ function ( require , module , exports ) {
@@ -849,7 +878,8 @@ module.exports = function (chai, _) {
849
878
, negate = flag ( this , 'negate' )
850
879
, ssfi = flag ( this , 'ssfi' )
851
880
, isDeep = flag ( this , 'deep' )
852
- , descriptor = isDeep ? 'deep ' : '' ;
881
+ , descriptor = isDeep ? 'deep ' : ''
882
+ , isEql = isDeep ? flag ( this , 'eql' ) : SameValueZero ;
853
883
854
884
flagMsg = flagMsg ? flagMsg + ': ' : '' ;
855
885
@@ -873,7 +903,6 @@ module.exports = function (chai, _) {
873
903
break ;
874
904
875
905
case 'map' :
876
- var isEql = isDeep ? _ . eql : SameValueZero ;
877
906
obj . forEach ( function ( item ) {
878
907
included = included || isEql ( item , val ) ;
879
908
} ) ;
@@ -882,7 +911,7 @@ module.exports = function (chai, _) {
882
911
case 'set' :
883
912
if ( isDeep ) {
884
913
obj . forEach ( function ( item ) {
885
- included = included || _ . eql ( item , val ) ;
914
+ included = included || isEql ( item , val ) ;
886
915
} ) ;
887
916
} else {
888
917
included = obj . has ( val ) ;
@@ -892,7 +921,7 @@ module.exports = function (chai, _) {
892
921
case 'array' :
893
922
if ( isDeep ) {
894
923
included = obj . some ( function ( item ) {
895
- return _ . eql ( item , val ) ;
924
+ return isEql ( item , val ) ;
896
925
} )
897
926
} else {
898
927
included = obj . indexOf ( val ) !== - 1 ;
@@ -1464,8 +1493,9 @@ module.exports = function (chai, _) {
1464
1493
1465
1494
function assertEql ( obj , msg ) {
1466
1495
if ( msg ) flag ( this , 'message' , msg ) ;
1496
+ var eql = flag ( this , 'eql' ) ;
1467
1497
this . assert (
1468
- _ . eql ( obj , flag ( this , 'object' ) )
1498
+ eql ( obj , flag ( this , 'object' ) )
1469
1499
, 'expected #{this} to deeply equal #{exp}'
1470
1500
, 'expected #{this} to not deeply equal #{exp}'
1471
1501
, obj
@@ -2233,7 +2263,8 @@ module.exports = function (chai, _) {
2233
2263
var isDeep = flag ( this , 'deep' )
2234
2264
, negate = flag ( this , 'negate' )
2235
2265
, pathInfo = isNested ? _ . getPathInfo ( obj , name ) : null
2236
- , value = isNested ? pathInfo . value : obj [ name ] ;
2266
+ , value = isNested ? pathInfo . value : obj [ name ]
2267
+ , isEql = isDeep ? flag ( this , 'eql' ) : ( val1 , val2 ) => val1 === val2 ; ;
2237
2268
2238
2269
var descriptor = '' ;
2239
2270
if ( isDeep ) descriptor += 'deep ' ;
@@ -2260,7 +2291,7 @@ module.exports = function (chai, _) {
2260
2291
2261
2292
if ( arguments . length > 1 ) {
2262
2293
this . assert (
2263
- hasProperty && ( isDeep ? _ . eql ( val , value ) : val === value )
2294
+ hasProperty && isEql ( val , value )
2264
2295
, 'expected #{this} to have ' + descriptor + _ . inspect ( name ) + ' of #{exp}, but got #{act}'
2265
2296
, 'expected #{this} to not have ' + descriptor + _ . inspect ( name ) + ' of #{act}'
2266
2297
, val
@@ -2408,9 +2439,10 @@ module.exports = function (chai, _) {
2408
2439
if ( msg ) flag ( this , 'message' , msg ) ;
2409
2440
var obj = flag ( this , 'object' ) ;
2410
2441
var actualDescriptor = Object . getOwnPropertyDescriptor ( Object ( obj ) , name ) ;
2442
+ var eql = flag ( this , 'eql' ) ;
2411
2443
if ( actualDescriptor && descriptor ) {
2412
2444
this . assert (
2413
- _ . eql ( descriptor , actualDescriptor )
2445
+ eql ( descriptor , actualDescriptor )
2414
2446
, 'expected the own property descriptor for ' + _ . inspect ( name ) + ' on #{this} to match ' + _ . inspect ( descriptor ) + ', got ' + _ . inspect ( actualDescriptor )
2415
2447
, 'expected the own property descriptor for ' + _ . inspect ( name ) + ' on #{this} to not match ' + _ . inspect ( descriptor )
2416
2448
, descriptor
@@ -2764,7 +2796,8 @@ module.exports = function (chai, _) {
2764
2796
var len = keys . length
2765
2797
, any = flag ( this , 'any' )
2766
2798
, all = flag ( this , 'all' )
2767
- , expected = keys ;
2799
+ , expected = keys
2800
+ , isEql = isDeep ? flag ( this , 'eql' ) : ( val1 , val2 ) => val1 === val2 ;
2768
2801
2769
2802
if ( ! any && ! all ) {
2770
2803
all = true ;
@@ -2774,11 +2807,7 @@ module.exports = function (chai, _) {
2774
2807
if ( any ) {
2775
2808
ok = expected . some ( function ( expectedKey ) {
2776
2809
return actual . some ( function ( actualKey ) {
2777
- if ( isDeep ) {
2778
- return _ . eql ( expectedKey , actualKey ) ;
2779
- } else {
2780
- return expectedKey === actualKey ;
2781
- }
2810
+ return isEql ( expectedKey , actualKey ) ;
2782
2811
} ) ;
2783
2812
} ) ;
2784
2813
}
@@ -2787,11 +2816,7 @@ module.exports = function (chai, _) {
2787
2816
if ( all ) {
2788
2817
ok = expected . every ( function ( expectedKey ) {
2789
2818
return actual . some ( function ( actualKey ) {
2790
- if ( isDeep ) {
2791
- return _ . eql ( expectedKey , actualKey ) ;
2792
- } else {
2793
- return expectedKey === actualKey ;
2794
- }
2819
+ return isEql ( expectedKey , actualKey ) ;
2795
2820
} ) ;
2796
2821
} ) ;
2797
2822
@@ -3479,7 +3504,7 @@ module.exports = function (chai, _) {
3479
3504
failNegateMsg = 'expected #{this} to not have the same ' + subject + ' as #{exp}' ;
3480
3505
}
3481
3506
3482
- var cmp = flag ( this , 'deep' ) ? _ . eql : undefined ;
3507
+ var cmp = flag ( this , 'deep' ) ? flag ( this , ' eql' ) : undefined ;
3483
3508
3484
3509
this . assert (
3485
3510
isSubsetOf ( subset , obj , cmp , contains , ordered )
@@ -3535,7 +3560,8 @@ module.exports = function (chai, _) {
3535
3560
, flagMsg = flag ( this , 'message' )
3536
3561
, ssfi = flag ( this , 'ssfi' )
3537
3562
, contains = flag ( this , 'contains' )
3538
- , isDeep = flag ( this , 'deep' ) ;
3563
+ , isDeep = flag ( this , 'deep' )
3564
+ , eql = flag ( this , 'eql' ) ;
3539
3565
new Assertion ( list , flagMsg , ssfi , true ) . to . be . an ( 'array' ) ;
3540
3566
3541
3567
if ( contains ) {
@@ -3549,7 +3575,7 @@ module.exports = function (chai, _) {
3549
3575
} else {
3550
3576
if ( isDeep ) {
3551
3577
this . assert (
3552
- list . some ( function ( possibility ) { return _ . eql ( expected , possibility ) } )
3578
+ list . some ( function ( possibility ) { return eql ( expected , possibility ) } )
3553
3579
, 'expected #{this} to deeply equal one of #{exp}'
3554
3580
, 'expected #{this} to deeply equal one of #{exp}'
3555
3581
, list
0 commit comments