Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect-utils): Fix deep equality of ImmutableJS Lists #12763

Merged
merged 2 commits into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
10 changes: 9 additions & 1 deletion packages/expect-utils/src/jasmineUtils.ts
Expand Up @@ -238,9 +238,10 @@ function isDomNode(obj: any): boolean {
);
}

// SENTINEL constants are from https://github.com/facebook/immutable-js
// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
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