@@ -497,6 +497,15 @@ function partiallyCompareSets(actual, expected, comparedObjects) {
497497 return true ;
498498}
499499
500+ // Helper function to get a unique key for 0, -0 to avoid collisions
501+ function getZeroKey ( item ) {
502+ if ( item === 0 ) {
503+ if ( ObjectIs ( item , - 0 ) ) return '-0' ;
504+ return '0' ;
505+ }
506+ return item ;
507+ }
508+
500509function partiallyCompareArrays ( actual , expected , comparedObjects ) {
501510 if ( expected . length > actual . length ) {
502511 return false ;
@@ -506,39 +515,66 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
506515
507516 // Create a map to count occurrences of each element in the expected array
508517 const expectedCounts = new SafeMap ( ) ;
509- for ( const expectedItem of expected ) {
510- let found = false ;
511- for ( const { 0 : key , 1 : count } of expectedCounts ) {
512- if ( isDeepStrictEqual ( key , expectedItem ) ) {
513- expectedCounts . set ( key , count + 1 ) ;
514- found = true ;
515- break ;
518+ const safeExpected = new SafeArrayIterator ( expected ) ;
519+
520+ for ( const expectedItem of safeExpected ) {
521+ // Check if the item is a zero or a -0, as these need to be handled separately
522+ if ( expectedItem === 0 ) {
523+ const zeroKey = getZeroKey ( expectedItem ) ;
524+ expectedCounts . set ( zeroKey , {
525+ count : ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ,
526+ expectedType : typeof expectedItem ,
527+ } ) ;
528+ } else {
529+ let found = false ;
530+ for ( const { 0 : key , 1 : { count, expectedType } } of expectedCounts ) {
531+ // eslint-disable-next-line valid-typeof
532+ if ( isDeepStrictEqual ( key , expectedItem ) && expectedType === typeof expectedItem ) {
533+ expectedCounts . set ( key , { count : count + 1 , expectedType } ) ;
534+ found = true ;
535+ break ;
536+ }
537+ }
538+ if ( ! found ) {
539+ expectedCounts . set ( expectedItem , { count : 1 , expectedType : typeof expectedItem } ) ;
516540 }
517- }
518- if ( ! found ) {
519- expectedCounts . set ( expectedItem , 1 ) ;
520541 }
521542 }
522543
523544 const safeActual = new SafeArrayIterator ( actual ) ;
524545
525- // Create a map to count occurrences of relevant elements in the actual array
526546 for ( const actualItem of safeActual ) {
527- for ( const { 0 : key , 1 : count } of expectedCounts ) {
528- if ( isDeepStrictEqual ( key , actualItem ) ) {
529- if ( count === 1 ) {
530- expectedCounts . delete ( key ) ;
531- } else {
532- expectedCounts . set ( key , count - 1 ) ;
547+ // Check if the item is a zero or a -0, as these need to be handled separately
548+ if ( actualItem === 0 ) {
549+ const zeroKey = getZeroKey ( actualItem ) ;
550+
551+ if ( expectedCounts . has ( zeroKey ) ) {
552+ const { count, expectedType } = expectedCounts . get ( zeroKey ) ;
553+ // eslint-disable-next-line valid-typeof
554+ if ( expectedType === typeof actualItem ) {
555+ if ( count === 1 ) {
556+ expectedCounts . delete ( zeroKey ) ;
557+ } else {
558+ expectedCounts . set ( zeroKey , { count : count - 1 , expectedType } ) ;
559+ }
560+ }
561+ }
562+ } else {
563+ for ( const { 0 : expectedItem , 1 : { count, expectedType } } of expectedCounts ) {
564+ // eslint-disable-next-line valid-typeof
565+ if ( isDeepStrictEqual ( expectedItem , actualItem ) && expectedType === typeof actualItem ) {
566+ if ( count === 1 ) {
567+ expectedCounts . delete ( expectedItem ) ;
568+ } else {
569+ expectedCounts . set ( expectedItem , { count : count - 1 , expectedType } ) ;
570+ }
571+ break ;
533572 }
534- break ;
535573 }
536574 }
537575 }
538576
539- const { size } = expectedCounts ;
540- expectedCounts . clear ( ) ;
541- return size === 0 ;
577+ return expectedCounts . size === 0 ;
542578}
543579
544580/**
0 commit comments