Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop getters and setters when diffing objects for error #9757

Merged
merged 14 commits into from Apr 3, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@
### Features

### Fixes

Copy link
Member Author

@SimenB SimenB Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing on master due to this missing newline, snuck it in here

- `[jest-matcher-utils]` Do not override properties with setters when diffing objects ([#9757](https://github.com/facebook/jest/pull/9757))
- `[@jest/watcher]` Correct return type of `shouldRunTestSuite` for `JestHookEmitter` ([#9753](https://github.com/facebook/jest/pull/9753))

### Chore & Maintenance
Expand Down
12 changes: 12 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Expand Up @@ -2082,6 +2082,18 @@ Expected: <g>"abc"</>
Received: <r>{"0": "a", "1": "b", "2": "c"}</>
`;

exports[`.toEqual() {pass: false} expect({"a": "foo"}).toEqual({"a": "bar"}) 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

<g>- Expected - 1</>
<r>+ Received + 1</>

<d> Object {</>
<g>- "a": "bar",</>
<r>+ "a": "foo",</>
<d> }</>
`;

exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContaining {"a": 2}) 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expand Down
11 changes: 11 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -435,6 +435,17 @@ describe('.toEqual()', () => {
[{a: 1}, {a: 2}],
[{a: 5}, {b: 6}],
[Object.freeze({foo: {bar: 1}}), {foo: {}}],
[
{
get a() {
return 'foo';
},
set a(value) {
throw new Error('noo');
},
},
{a: 'bar'},
],
['banana', 'apple'],
['1\u{00A0}234,57\u{00A0}$', '1 234,57 $'], // issues/6881
[
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-matcher-utils/src/Replaceable.ts
Expand Up @@ -37,7 +37,7 @@ export default class Replaceable {
});
Object.getOwnPropertySymbols(this.object).forEach(key => {
const descriptor = Object.getOwnPropertyDescriptor(this.object, key);
if ((descriptor as PropertyDescriptor).enumerable) {

This comment was marked as outdated.

if (descriptor?.enumerable) {
cb(this.object[key], key, this.object);
}
});
Expand All @@ -57,7 +57,12 @@ export default class Replaceable {
if (this.type === 'map') {
this.object.set(key, value);
} else {
this.object[key] = value;
const descriptor = Object.getOwnPropertyDescriptor(this.object, key);

// do not try to assign to anything that has a setter
if (descriptor?.set === undefined) {
this.object[key] = value;
}
}
}
}
Expand Down