Skip to content

Commit

Permalink
fix: equality of iterable with symbols (#14688)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniAcu authored Nov 16, 2023
1 parent 5b97c9d commit 540c239
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))
- `[@jest/expect-utils]` Fix comparison of `URL` ([#14672](https://github.com/jestjs/jest/pull/14672))
- `[@jest/expect-utils]` Check `Symbol` properties in equality ([#14688](https://github.com/jestjs/jest/pull/14688))
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
- `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589))
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
Expand Down
30 changes: 30 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,36 @@ describe('iterableEquality', () => {
const b = new TestRecord().set('dummy', 'data');
expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given a symbols keys within equal objects', () => {
const KEY = Symbol();

const a = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
[KEY]: [],
};
const b = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
[KEY]: [],
};

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

test('returns false when given a symbols keys within inequal objects', () => {
const KEY = Symbol();

const a = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
[KEY]: [1],
};
const b = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
[KEY]: [],
};

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

describe('typeEquality', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ export const iterableEquality = (
!isImmutableOrderedSet(a) &&
!isImmutableRecord(a)
) {
const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
const aEntries = entries(a);
const bEntries = entries(b);
if (!equals(aEntries, bEntries)) {
return false;
}
Expand All @@ -320,6 +320,15 @@ export const iterableEquality = (
return true;
};

const entries = (obj: any) => {
if (!isObject(obj)) return [];

return Object.getOwnPropertySymbols(obj)
.filter(key => key !== Symbol.iterator)
.map(key => [key, obj[key]])
.concat(Object.entries(obj));
};

const isObject = (a: any) => a !== null && typeof a === 'object';

const isObjectWithKeys = (a: any) =>
Expand Down

0 comments on commit 540c239

Please sign in to comment.