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
72 changes: 72 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Expand Up @@ -2127,6 +2127,66 @@ exports[`.toEqual() {pass: false} expect({"foo": {"bar": 1}}).toEqual({"foo": {}
<d> }</>
`;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

exports[`.toEqual() {pass: false} expect({"nodeName": "div", "nodeType": 1}).toEqual({"nodeName": "p", "nodeType": 1}) 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expand All @@ -2140,6 +2200,18 @@ exports[`.toEqual() {pass: false} expect({"nodeName": "div", "nodeType": 1}).toE
<d> }</>
`;

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

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

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

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

Expand Down
56 changes: 56 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -435,6 +435,62 @@ describe('.toEqual()', () => {
[{a: 1}, {a: 2}],
[{a: 5}, {b: 6}],
[Object.freeze({foo: {bar: 1}}), {foo: {}}],
[
{
get getterAndSetter() {
return 'foo';
},
set getterAndSetter(value) {
throw new Error('noo');
},
},
{getterAndSetter: 'bar'},
],
[
Object.freeze({
get frozenGetterAndSetter() {
return 'foo';
},
set frozenGetterAndSetter(value) {
throw new Error('noo');
},
}),
{frozenGetterAndSetter: 'bar'},
],
[
{
get getter() {
return 'foo';
},
},
{getter: 'bar'},
],
[
Object.freeze({
get frozenGetter() {
return 'foo';
},
}),
{frozenGetter: 'bar'},
],
[
{
// eslint-disable-next-line accessor-pairs
set setter(value) {
throw new Error('noo');
},
},
{setter: 'bar'},
],
[
Object.freeze({
// eslint-disable-next-line accessor-pairs
set frozenSetter(value) {
throw new Error('noo');
},
}),
{frozenSetter: 'bar'},
],
['banana', 'apple'],
['1\u{00A0}234,57\u{00A0}$', '1 234,57 $'], // issues/6881
[
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-matcher-utils/src/Replaceable.ts
Expand Up @@ -36,10 +36,7 @@ export default class Replaceable {
cb(value, key, this.object);
});
Object.getOwnPropertySymbols(this.object).forEach(key => {
const descriptor = Object.getOwnPropertyDescriptor(this.object, key);
if ((descriptor as PropertyDescriptor).enumerable) {

This comment was marked as outdated.

cb(this.object[key], key, this.object);
}
cb(this.object[key], key, this.object);
});
} else {
this.object.forEach(cb);
Expand Down
23 changes: 12 additions & 11 deletions packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
Expand Up @@ -49,21 +49,22 @@ export default function deepCyclicCopyReplaceable<T>(

function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, any>): T {
const newObject = Object.create(Object.getPrototypeOf(object));
const descriptors = Object.getOwnPropertyDescriptors(object);
const descriptors: {
[x: string]: PropertyDescriptor;
} = Object.getOwnPropertyDescriptors(object);

cycles.set(object, newObject);

Object.keys(descriptors).forEach(key => {
const descriptor = descriptors[key];
if (typeof descriptor.value !== 'undefined') {
descriptor.value = deepCyclicCopyReplaceable(descriptor.value, cycles);
}

if (!('set' in descriptor)) {
descriptor.writable = true;
}

descriptor.configurable = true;
descriptors[key] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the enumerable check be here instead?

if (descriptors[key].enumerable) {
  descriptors[key] = ...
} else {
  delete descriptors[key];
}

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.

in that case we lose Symbol keys. Not sure why...

image

Copy link
Member Author

Choose a reason for hiding this comment

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

At least if I understood your comment correcly

diff --git i/packages/jest-matcher-utils/src/Replaceable.ts w/packages/jest-matcher-utils/src/Replaceable.ts
index fc8c7343c..dd9e04f32 100644
--- i/packages/jest-matcher-utils/src/Replaceable.ts
+++ w/packages/jest-matcher-utils/src/Replaceable.ts
@@ -35,12 +35,6 @@ export default class Replaceable {
       Object.entries(this.object).forEach(([key, value]) => {
         cb(value, key, this.object);
       });
-      Object.getOwnPropertySymbols(this.object).forEach(key => {
-        const descriptor = Object.getOwnPropertyDescriptor(this.object, key);
-        if (descriptor?.enumerable) {
-          cb(this.object[key], key, this.object);
-        }
-      });
     } else {
       this.object.forEach(cb);
     }
diff --git i/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts w/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
index ad98131dd..67271d5d8 100644
--- i/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
+++ w/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
@@ -56,15 +56,19 @@ function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, any>): T {
   cycles.set(object, newObject);
 
   Object.keys(descriptors).forEach(key => {
-    descriptors[key] = {
-      configurable: true,
-      enumerable: true,
-      value: deepCyclicCopyReplaceable(
-        (object as Record<string, unknown>)[key],
-        cycles,
-      ),
-      writable: true,
-    };
+    if (descriptors[key].enumerable) {
+      descriptors[key] = descriptors[key] = {
+        configurable: true,
+        enumerable: true,
+        value: deepCyclicCopyReplaceable(
+          (object as Record<string, unknown>)[key],
+          cycles,
+        ),
+        writable: true,
+      };
+    } else {
+      delete descriptors[key];
+    }
   });
 
   return Object.defineProperties(newObject, descriptors);

Copy link
Contributor

Choose a reason for hiding this comment

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

You'd still need the Object.getOwnPropertySymbols call but you can lose the if (descriptor?.enumerable) { check because we would've not copied those over in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

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

right! done 🙂

configurable: true,
enumerable: true,
value: deepCyclicCopyReplaceable(
(object as Record<string, unknown>)[key],
Copy link
Member Author

Choose a reason for hiding this comment

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

without casting
image

not sure if it's solvable?

cycles,
),
writable: true,
};
});

return Object.defineProperties(newObject, descriptors);
Expand Down