Skip to content

Commit dd690ca

Browse files
authored
fix(expect-utils): Treat ImmutableJS Lists as Sets (#12763)
1 parent e9cc8a8 commit dd690ca

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Fixes
88

9+
- `[@jest/expect-utils]` Fix deep equality of ImmutableJS Lists ([#12763](https://github.com/facebook/jest/pull/12763))
10+
911
### Chore & Maintenance
1012

1113
- `[@jest-reporters]` Move helper functions from `utils.ts` into separate files ([#12782](https://github.com/facebook/jest/pull/12782))

packages/expect-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jest-get-type": "^28.0.2"
2121
},
2222
"devDependencies": {
23+
"immutable": "^4.0.0",
2324
"jest-matcher-utils": "^28.0.2"
2425
},
2526
"engines": {

packages/expect-utils/src/__tests__/utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88

9+
import {List} from 'immutable';
910
import {stringify} from 'jest-matcher-utils';
1011
import {
1112
arrayBufferEquality,
@@ -517,6 +518,13 @@ describe('iterableEquality', () => {
517518

518519
expect(iterableEquality(a, b)).toBe(true);
519520
});
521+
522+
test('returns true when given Immutable Lists without an OwnerID', () => {
523+
const a = List([1, 2, 3]);
524+
const b = a.filter(v => v > 0);
525+
526+
expect(iterableEquality(a, b)).toBe(true);
527+
});
520528
});
521529

522530
describe('arrayBufferEquality', () => {

packages/expect-utils/src/jasmineUtils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ function isDomNode(obj: any): boolean {
238238
);
239239
}
240240

241-
// SENTINEL constants are from https://github.com/facebook/immutable-js
241+
// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
242242
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
243243
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
244+
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
244245
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
245246

246247
export function isImmutableUnorderedKeyed(maybeKeyed: any) {
@@ -258,3 +259,10 @@ export function isImmutableUnorderedSet(maybeSet: any) {
258259
!maybeSet[IS_ORDERED_SENTINEL]
259260
);
260261
}
262+
263+
export function isImmutableList(maybeList: any) {
264+
return !!(
265+
maybeList &&
266+
maybeList[IS_LIST_SENTINEL]
267+
);
268+
}

packages/expect-utils/src/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {isPrimitive} from 'jest-get-type';
1010
import {
1111
equals,
1212
isA,
13+
isImmutableList,
1314
isImmutableUnorderedKeyed,
1415
isImmutableUnorderedSet,
1516
} from './jasmineUtils';
@@ -254,10 +255,12 @@ export const iterableEquality = (
254255
return false;
255256
}
256257

257-
const aEntries = Object.entries(a);
258-
const bEntries = Object.entries(b);
259-
if (!equals(aEntries, bEntries)) {
260-
return false;
258+
if (!isImmutableList(a)) {
259+
const aEntries = Object.entries(a);
260+
const bEntries = Object.entries(b);
261+
if (!equals(aEntries, bEntries)) {
262+
return false;
263+
}
261264
}
262265

263266
// Remove the first value from the stack of traversed values.

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,7 @@ __metadata:
26062606
version: 0.0.0-use.local
26072607
resolution: "@jest/expect-utils@workspace:packages/expect-utils"
26082608
dependencies:
2609+
immutable: ^4.0.0
26092610
jest-get-type: ^28.0.2
26102611
jest-matcher-utils: ^28.0.2
26112612
languageName: unknown

0 commit comments

Comments
 (0)