From a34fcb80d3e50e7675bb4dca9e31d0345cc09297 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 5 Jun 2018 09:42:09 +0200 Subject: [PATCH 1/4] `expect` no longer tries to equal non-enumerable symbolic properties. Fixes #6392 --- CHANGELOG.md | 4 ++++ .../expect/src/__tests__/matchers.test.js | 23 +++++++++++++++++++ packages/expect/src/jasmine_utils.js | 6 ++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 522e3fbd349e..7cb94ad25a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## master +### Fixes + +- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([]()) + ## 23.1.0 ### Features diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 69e04a4e79b8..30a3f48d75fe 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -442,6 +442,29 @@ describe('.toEqual()', () => { ); } }); + + test('non-enumerable members should be skipped during equal', () => { + const actual = { + x: 3 + }; + Object.defineProperty(actual, "test", { + enumerable: false, + value: 5 + }); + expect(actual).toEqual({ x: 3 }); + }); + + test('non-enumerable symbolic members should be skipped during equal', () => { + const actual = { + x: 3 + }; + const mySymbol = Symbol("test"); + Object.defineProperty(actual, mySymbol, { + enumerable: false, + value: 5 + }); + expect(actual).toEqual({ x: 3 }); + }); }); describe('.toBeInstanceOf()', () => { diff --git a/packages/expect/src/jasmine_utils.js b/packages/expect/src/jasmine_utils.js index 6fc2f6d2e89f..7306c462cc80 100644 --- a/packages/expect/src/jasmine_utils.js +++ b/packages/expect/src/jasmine_utils.js @@ -209,7 +209,11 @@ function keys(obj, isArray, hasKey) { keys.push(key); } } - return keys.concat((Object.getOwnPropertySymbols(o): Array)); + return keys.concat( + (Object.getOwnPropertySymbols(o): Array).filter( + symbol => Object.getOwnPropertyDescriptor(o, symbol).enumerable, + ), + ); })(obj); if (!isArray) { From 0b646081cc752958e69f68ef5f8b7056c44afc86 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 5 Jun 2018 09:54:03 +0200 Subject: [PATCH 2/4] Updated changelog with PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cb94ad25a61..f77aa582ee69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Fixes -- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([]()) +- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398)) ## 23.1.0 From c2bcd41c4c5a97b4cee317c03c14885b3085cc2d Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 5 Jun 2018 10:11:54 +0200 Subject: [PATCH 3/4] Fixed linting errors --- .../expect/src/__tests__/matchers.test.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 30a3f48d75fe..08aa419cc767 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -445,25 +445,25 @@ describe('.toEqual()', () => { test('non-enumerable members should be skipped during equal', () => { const actual = { - x: 3 + x: 3, }; - Object.defineProperty(actual, "test", { - enumerable: false, - value: 5 + Object.defineProperty(actual, 'test', { + enumerable: false, + value: 5, }); - expect(actual).toEqual({ x: 3 }); + expect(actual).toEqual({x: 3}); }); test('non-enumerable symbolic members should be skipped during equal', () => { const actual = { - x: 3 + x: 3, }; - const mySymbol = Symbol("test"); + const mySymbol = Symbol('test'); Object.defineProperty(actual, mySymbol, { - enumerable: false, - value: 5 + enumerable: false, + value: 5, }); - expect(actual).toEqual({ x: 3 }); + expect(actual).toEqual({x: 3}); }); }); From 8efad97ead951125a47a891e77a4a0f27a004b3d Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 5 Jun 2018 11:21:19 +0200 Subject: [PATCH 4/4] 'fixed' jest error --- packages/expect/src/jasmine_utils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/expect/src/jasmine_utils.js b/packages/expect/src/jasmine_utils.js index 7306c462cc80..de423960f58a 100644 --- a/packages/expect/src/jasmine_utils.js +++ b/packages/expect/src/jasmine_utils.js @@ -211,6 +211,7 @@ function keys(obj, isArray, hasKey) { } return keys.concat( (Object.getOwnPropertySymbols(o): Array).filter( + //$FlowFixMe Jest complains about nullability, but we know for sure that property 'symbol' does exist. symbol => Object.getOwnPropertyDescriptor(o, symbol).enumerable, ), );