diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f7984aa970..89d071ccc605 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index 66394ac071cf..5f11d9c306c2 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -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; @@ -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]); diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index d85497f21152..39d4b3048b71 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -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, + }); + }); });