Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: toMatchObject works with super getters
Browse files Browse the repository at this point in the history
- It also works with `defineProperty`
- There's still a bit of work to be done on
  improving the reporting of this
souldzin committed Aug 7, 2020

Verified

This commit was signed with the committer’s verified signature. The key has expired.
renovate-bot Mend Renovate
1 parent c95abca commit 9572967
Showing 3 changed files with 39 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
@@ -4314,13 +4314,27 @@ exports[`toMatchObject() {pass: true} expect({"a": undefined}).toMatchObject({"a
Expected: not <g>{"a": undefined}</>
`;

exports[`toMatchObject() {pass: true} expect({}).toMatchObject({"a": undefined, "b": "b", "c": "c"}) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toMatchObject<d>(</><g>expected</><d>)</>

Expected: not <g>{"a": undefined, "b": "b", "c": "c"}</>
Received: <r>{}</>
`;

exports[`toMatchObject() {pass: true} expect({}).toMatchObject({"a": undefined, "b": "b"}) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toMatchObject<d>(</><g>expected</><d>)</>

Expected: not <g>{"a": undefined, "b": "b"}</>
Received: <r>{}</>
`;

exports[`toMatchObject() {pass: true} expect({}).toMatchObject({"d": 4}) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toMatchObject<d>(</><g>expected</><d>)</>

Expected: not <g>{"d": 4}</>
Received: <r>{}</>
`;

exports[`toMatchObject() {pass: true} expect(2015-11-30T00:00:00.000Z).toMatchObject(2015-11-30T00:00:00.000Z) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toMatchObject<d>(</><g>expected</><d>)</>

21 changes: 21 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
@@ -1958,6 +1958,22 @@ describe('toMatchObject()', () => {
}
}

class Sub extends Foo {
get c() {
return 'c';
}
}

const withDefineProperty = (obj, key, val) => {
Object.defineProperty(obj, key, {
get() {
return val;
},
});

return obj;
};

const testNotToMatchSnapshots = tuples => {
tuples.forEach(([n1, n2]) => {
it(`{pass: true} expect(${stringify(n1)}).toMatchObject(${stringify(
@@ -2082,6 +2098,11 @@ describe('toMatchObject()', () => {
{a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'},
{a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'},
],
// These snapshots will show {} as the object because the properties
// are not enumerable. We will need to somehow make the serialization of
// these keys a little smarter before reporting accurately.
[new Sub(), {a: undefined, b: 'b', c: 'c'}],
[withDefineProperty(new Sub(), 'd', 4), {d: 4}],
]);

testToMatchSnapshots([
5 changes: 4 additions & 1 deletion packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ type GetPath = {
value?: unknown;
};

const hasPropertyInObject = (object: object, key: string) =>
typeof object === 'object' && key in object;

// Return whether object instance inherits getter from its class.
const hasGetterFromConstructor = (object: object, key: string) => {
const constructor = object.constructor;
@@ -299,7 +302,7 @@ export const subsetEquality = (
}
const result =
object != null &&
hasOwnProperty(object, key) &&
hasPropertyInObject(object, key) &&
equals(object[key], subset[key], [
iterableEquality,
subsetEqualityWithContext(seenReferences),

0 comments on commit 9572967

Please sign in to comment.