Skip to content

Commit

Permalink
Fix: Symbol based properties in arrays causes toEqual to throw (#6391)
Browse files Browse the repository at this point in the history
* Fixed issue where jest fails to compare symbol properties in arrays

* Updated changelog

* fixup changelog

* Fixed linting errors

* Supress incorrect flow error
  • Loading branch information
mweststrate authored and cpojer committed Jun 5, 2018
1 parent e82a933 commit 05bbcc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/expect/src/jasmine_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down

0 comments on commit 05bbcc5

Please sign in to comment.