From 6b4c8cf08ad0a3625e2780f3fd1e7bbb626fa564 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Fri, 25 Nov 2022 14:10:16 +0100 Subject: [PATCH 1/4] fix: expect.toMatchObject ignores symbol key properties --- packages/expect-utils/src/utils.ts | 4 ++-- packages/expect/src/__tests__/matchers.test.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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..10b9810ae9f2 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'); + 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, + }); + }); }); From 043d719336938c471273a293b5f86d5a276c866e Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Sat, 26 Nov 2022 09:53:32 +0100 Subject: [PATCH 2/4] fix: type check failing in typecheck:tests --- packages/jest-snapshot/src/__tests__/mockSerializer.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/mockSerializer.test.ts b/packages/jest-snapshot/src/__tests__/mockSerializer.test.ts index e489519a37fb..454229f9066d 100644 --- a/packages/jest-snapshot/src/__tests__/mockSerializer.test.ts +++ b/packages/jest-snapshot/src/__tests__/mockSerializer.test.ts @@ -14,7 +14,7 @@ test('mock with 0 calls and default name', () => { }); test('mock with 2 calls, 1 return, 1 throw', () => { - const fn = jest.fn(value => { + const fn = jest.fn((value: number) => { if (value % 2 === 0) { return value * 2; } else { From 3887b03da257a79b49ad93c809ebd418b5f35385 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Dec 2022 11:37:10 +0100 Subject: [PATCH 3/4] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From cee142711dd1da2692412424ccce4c68ff43b99d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Dec 2022 11:37:31 +0100 Subject: [PATCH 4/4] use jestexpect --- packages/expect/src/__tests__/matchers.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 10b9810ae9f2..39d4b3048b71 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -2320,15 +2320,15 @@ describe('toMatchObject()', () => { // 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({ + 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, }); - expect({[sym]: true}).toMatchObject({[sym]: true}); - expect({example: 10, [sym]: true}).toMatchObject({ + jestExpect({[sym]: true}).toMatchObject({[sym]: true}); + jestExpect({example: 10, [sym]: true}).toMatchObject({ example: 10, [sym]: true, });