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: not addressing to Sets and Maps as object without keys #14873

Merged
merged 2 commits into from
Apr 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- `[@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/expect-utils]` Catch circular references within arrays when matching objects ([#14894](https://github.com/jestjs/jest/pull/14894))
- `[@jest/expect-utils]` Fix not addressing to Sets and Maps as objects without keys ([#14873](https://github.com/jestjs/jest/pull/14873))
- `[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
31 changes: 31 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,37 @@ describe('subsetEquality()', () => {
});
});
});

describe('subset is not object with keys', () => {
test('returns true if subset has keys', () => {
expect(subsetEquality({foo: 'bar'}, {foo: 'bar'})).toBe(true);
});
test('returns true if subset has Symbols', () => {
const symbol = Symbol('foo');
expect(subsetEquality({[symbol]: 'bar'}, {[symbol]: 'bar'})).toBe(true);
});
test('returns undefined if subset has no keys', () => {
expect(subsetEquality('foo', 'bar')).toBeUndefined();
});
test('returns undefined if subset is null', () => {
expect(subsetEquality({foo: 'bar'}, null)).toBeUndefined();
});
test('returns undefined if subset is Error', () => {
expect(subsetEquality({foo: 'bar'}, new Error())).toBeUndefined();
});
test('returns undefined if subset is Array', () => {
expect(subsetEquality({foo: 'bar'}, [])).toBeUndefined();
});
test('returns undefined if subset is Date', () => {
expect(subsetEquality({foo: 'bar'}, new Date())).toBeUndefined();
});
test('returns undefined if subset is Set', () => {
expect(subsetEquality({foo: 'bar'}, new Set())).toBeUndefined();
});
test('returns undefined if subset is Map', () => {
expect(subsetEquality({foo: 'bar'}, new Map())).toBeUndefined();
});
});
});

describe('iterableEquality', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ const isObjectWithKeys = (a: any) =>
isObject(a) &&
!(a instanceof Error) &&
!Array.isArray(a) &&
!(a instanceof Date);
!(a instanceof Date) &&
!(a instanceof Set) &&
!(a instanceof Map);

export const subsetEquality = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of subset as a Set the getObjectKeys(subset) at line 358 returns empty array ([]), so the .every(...) in the same line is vacuous truth, resulting the comparison not to raise an exception

object: unknown,
Expand Down
Loading