Skip to content

Commit

Permalink
fix: expect.toMatchObject ignores symbol key properties (#13639)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Dec 31, 2022
1 parent fb2de8a commit fb37bd1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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,
});
});
});

0 comments on commit fb37bd1

Please sign in to comment.