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.toMatchObject ignores symbol key properties #13639

Merged
merged 5 commits into from Dec 31, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,8 @@

### Fixes

- `[jest-resolve]` add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633))
- `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639))
- `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633))

### Chore & Maintenance

Expand Down
4 changes: 2 additions & 2 deletions packages/expect-utils/src/utils.ts
Expand Up @@ -28,7 +28,7 @@ type GetPath = {
/**
* Checks if `hasOwnProperty(object, key)` up the prototype chain, stopping at `Object.prototype`.
*/
const hasPropertyInObject = (object: object, key: string): boolean => {
const hasPropertyInObject = (object: object, key: string | symbol): boolean => {
const shouldTerminate =
!object || typeof object !== 'object' || object === Object.prototype;

Expand Down Expand Up @@ -301,7 +301,7 @@ export const subsetEquality = (
return undefined;
}

return Object.keys(subset).every(key => {
return Reflect.ownKeys(subset).every(key => {
if (isObjectWithKeys(subset[key])) {
if (seenReferences.has(subset[key])) {
return equals(object[key], subset[key], [iterableEquality]);
Expand Down
18 changes: 18 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -2315,4 +2315,22 @@ describe('toMatchObject()', () => {
jestExpect(b).toMatchObject(matcher),
).toThrowErrorMatchingSnapshot();
});

it('toMatchObject ignores symbol key properties', () => {
// issue 13638
const sym = Symbol('foo');
const sym2 = Symbol('foo2');
jestExpect({}).not.toMatchObject({[sym]: true});
jestExpect({[sym]: true}).not.toMatchObject({[sym2]: true});
jestExpect({[sym]: true}).not.toMatchObject({[sym]: false});
jestExpect({example: 10, [sym]: true}).not.toMatchObject({
example: 12,
[sym]: true,
});
jestExpect({[sym]: true}).toMatchObject({[sym]: true});
jestExpect({example: 10, [sym]: true}).toMatchObject({
example: 10,
[sym]: true,
});
});
});