Skip to content

Commit

Permalink
Fix: expect no longer tries to equal non-enumerable symbolic proper…
Browse files Browse the repository at this point in the history
…ties.… (#6398)

* `expect` no longer tries to equal non-enumerable symbolic properties. Fixes #6392

* Updated changelog with PR number

* Fixed linting errors

* 'fixed' jest error
  • Loading branch information
mweststrate authored and cpojer committed Jun 5, 2018
1 parent 241588a commit e82a933
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Fixes

- `[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

### Features
Expand Down
23 changes: 23 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/expect/src/jasmine_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ function keys(obj, isArray, hasKey) {
keys.push(key);
}
}
return keys.concat((Object.getOwnPropertySymbols(o): Array<any>));
return keys.concat(
(Object.getOwnPropertySymbols(o): Array<any>).filter(
//$FlowFixMe Jest complains about nullability, but we know for sure that property 'symbol' does exist.
symbol => Object.getOwnPropertyDescriptor(o, symbol).enumerable,
),
);
})(obj);

if (!isArray) {
Expand Down

0 comments on commit e82a933

Please sign in to comment.