Skip to content

Commit

Permalink
fix(expect-utils): Skip ImmutableList entries check
Browse files Browse the repository at this point in the history
Some of their internal properties may not match, but they should be
considered equal.
  • Loading branch information
romellem committed Apr 29, 2022
1 parent c8c32d3 commit 8e11fcc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

### Fixes

- `[@jest/expect-utils]` Fix deep equality of ImmutableJS Lists ([#12763](https://github.com/facebook/jest/pull/12763))

### Chore & Maintenance

### Performance
Expand Down
1 change: 1 addition & 0 deletions packages/expect-utils/package.json
Expand Up @@ -20,6 +20,7 @@
"jest-get-type": "^28.0.2"
},
"devDependencies": {
"immutable": "^4.0.0",
"jest-matcher-utils": "^28.0.2"
},
"engines": {
Expand Down
8 changes: 8 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Expand Up @@ -6,6 +6,7 @@
*
*/

import {List} from 'immutable';
import {stringify} from 'jest-matcher-utils';
import {
arrayBufferEquality,
Expand Down Expand Up @@ -517,6 +518,13 @@ describe('iterableEquality', () => {

expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given Immutable Lists without an OwnerID', () => {
const a = List([1, 2, 3]);
const b = a.filter(v => v > 0);

expect(iterableEquality(a, b)).toBe(true);
});
});

describe('arrayBufferEquality', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/expect-utils/src/jasmineUtils.ts
Expand Up @@ -241,6 +241,7 @@ function isDomNode(obj: any): boolean {
// SENTINEL constants are from https://github.com/facebook/immutable-js
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';

export function isImmutableUnorderedKeyed(maybeKeyed: any) {
Expand All @@ -258,3 +259,10 @@ export function isImmutableUnorderedSet(maybeSet: any) {
!maybeSet[IS_ORDERED_SENTINEL]
);
}

export function isImmutableList(maybeList: any) {
return !!(
maybeList &&
maybeList[IS_LIST_SENTINEL]
);
}
11 changes: 7 additions & 4 deletions packages/expect-utils/src/utils.ts
Expand Up @@ -10,6 +10,7 @@ import {isPrimitive} from 'jest-get-type';
import {
equals,
isA,
isImmutableList,
isImmutableUnorderedKeyed,
isImmutableUnorderedSet,
} from './jasmineUtils';
Expand Down Expand Up @@ -254,10 +255,12 @@ export const iterableEquality = (
return false;
}

const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (!equals(aEntries, bEntries)) {
return false;
if (!isImmutableList(a)) {
const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (!equals(aEntries, bEntries)) {
return false;
}
}

// Remove the first value from the stack of traversed values.
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -2606,6 +2606,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@jest/expect-utils@workspace:packages/expect-utils"
dependencies:
immutable: ^4.0.0
jest-get-type: ^28.0.2
jest-matcher-utils: ^28.0.2
languageName: unknown
Expand Down

0 comments on commit 8e11fcc

Please sign in to comment.