From 05bbcc55003d0234d5647517787407a847cd941c Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 5 Jun 2018 22:27:10 +0200 Subject: [PATCH] Fix: Symbol based properties in arrays causes `toEqual` to throw (#6391) * Fixed issue where jest fails to compare symbol properties in arrays * Updated changelog * fixup changelog * Fixed linting errors * Supress incorrect flow error --- CHANGELOG.md | 1 + packages/expect/src/__tests__/matchers.test.js | 13 +++++++++++++ packages/expect/src/jasmine_utils.js | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f77aa582ee69..60541aec1eb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Fixes +- `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391)) - `[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 diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 08aa419cc767..9c331c36e07e 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -443,6 +443,19 @@ describe('.toEqual()', () => { } }); + test('symbol based keys in arrays are processed correctly', () => { + const mySymbol = Symbol('test'); + const actual1 = []; + actual1[mySymbol] = 3; + const actual2 = []; + actual2[mySymbol] = 4; + const expected = []; + expected[mySymbol] = 3; + + expect(actual1).toEqual(expected); + expect(actual2).not.toEqual(expected); + }); + test('non-enumerable members should be skipped during equal', () => { const actual = { x: 3, diff --git a/packages/expect/src/jasmine_utils.js b/packages/expect/src/jasmine_utils.js index de423960f58a..144374f0cdff 100644 --- a/packages/expect/src/jasmine_utils.js +++ b/packages/expect/src/jasmine_utils.js @@ -227,7 +227,8 @@ function keys(obj, isArray, hasKey) { } for (var x = 0; x < allKeys.length; x++) { - if (!allKeys[x].match(/^[0-9]+$/)) { + //$FlowFixMe + if (typeof allKeys[x] === 'symbol' || !allKeys[x].match(/^[0-9]+$/)) { extraKeys.push(allKeys[x]); } }