Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: expect.toMatchObject ignores symbol key properties
  • Loading branch information
lpizzinidev committed Nov 25, 2022
commit 6b4c8cf08ad0a3625e2780f3fd1e7bbb626fa564
4 changes: 2 additions & 2 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
expect({}).not.toMatchObject({[sym]: true});
expect({[sym]: true}).not.toMatchObject({[sym2]: true});
expect({[sym]: true}).not.toMatchObject({[sym]: false});
expect({example: 10, [sym]: true}).not.toMatchObject({
example: 12,
[sym]: true,
});
expect({[sym]: true}).toMatchObject({[sym]: true});
expect({example: 10, [sym]: true}).toMatchObject({
example: 10,
[sym]: true,
});
});
});