Skip to content

Commit

Permalink
fix: handle circular references correctly in objects (closes jestjs#8663
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lucasfcosta committed Jul 13, 2019
1 parent b76f01b commit e087fc6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
24 changes: 24 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4092,6 +4092,30 @@ Expected: not <green>Set {2, 1}</>
Received: <red>Set {1, 2}</>"
`;

exports[`toMatchObject() circular references simple circular references 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toMatchObject<dim>(</><green>expected</><dim>)</>

Expected: not <green>{\\"ref\\": [Circular]}</>"
`;

exports[`toMatchObject() circular references simple circular references 2`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toMatchObject<dim>(</><green>expected</><dim>)</>

Expected: not <green>[[Circular]]</>"
`;

exports[`toMatchObject() circular references transitive circular references 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toMatchObject<dim>(</><green>expected</><dim>)</>

Expected: not <green>{\\"nestedObj\\": {\\"parentObj\\": [Circular]}}</>"
`;

exports[`toMatchObject() circular references transitive circular references 2`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toMatchObject<dim>(</><green>expected</><dim>)</>

Expected: not <green>[[[Circular]]]</>"
`;

exports[`toMatchObject() throws expect("44").toMatchObject({}) 1`] = `
"<dim>expect(</><red>received</><dim>).</>toMatchObject<dim>(</><green>expected</><dim>)</>

Expand Down
43 changes: 43 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,49 @@ describe('toMatchObject()', () => {
}
}

describe('circular references', () => {
test('simple circular references', () => {
const circularObj = {};
circularObj.ref = circularObj;

jestExpect(circularObj).toMatchObject(circularObj);
expect(() => {
jestExpect(circularObj).not.toMatchObject(circularObj);
}).toThrowErrorMatchingSnapshot();

const circularArray = [];
circularArray.push(circularArray);
jestExpect(circularArray).toMatchObject(circularArray);
expect(() => {
jestExpect(circularArray).not.toMatchObject(circularArray);
}).toThrowErrorMatchingSnapshot();
});

test('transitive circular references', () => {
const transitiveCircularObj = {};
transitiveCircularObj.nestedObj = {parentObj: transitiveCircularObj};

jestExpect(transitiveCircularObj).toMatchObject(transitiveCircularObj);
expect(() => {
jestExpect(transitiveCircularObj).not.toMatchObject(
transitiveCircularObj,
);
}).toThrowErrorMatchingSnapshot();

const transitiveCircularArray = [];
const refArray = [transitiveCircularArray];
transitiveCircularArray.push(refArray);
jestExpect(transitiveCircularArray).toMatchObject(
transitiveCircularArray,
);
expect(() => {
jestExpect(transitiveCircularArray).not.toMatchObject(
transitiveCircularArray,
);
}).toThrowErrorMatchingSnapshot();
});
});

[
[{a: 'b', c: 'd'}, {a: 'b'}],
[{a: 'b', c: 'd'}, {a: 'b', c: 'd'}],
Expand Down
45 changes: 34 additions & 11 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@ export const iterableEquality = (
return true;
};

const isObject = (a: any) => a !== null && typeof a === 'object';

const isObjectWithKeys = (a: any) =>
a !== null &&
typeof a === 'object' &&
isObject(a) &&
!(a instanceof Error) &&
!(a instanceof Array) &&
!(a instanceof Date);
Expand All @@ -268,16 +269,38 @@ export const subsetEquality = (
object: any,
subset: any,
): undefined | boolean => {
if (!isObjectWithKeys(subset)) {
return undefined;
}
const seenReferences = new WeakMap();

// subsetEquality needs to keep track of the references
// it has already visited to avoid infinite loops in case
// there are circular references in the subset passed to it.
const subsetEqualityWithContext = (
seenReferences: WeakMap<object, boolean>,
) => (object: any, subset: any): undefined | boolean => {
if (!isObjectWithKeys(subset)) {
return undefined;
}

return Object.keys(subset).every(
key =>
object != null &&
hasOwnProperty(object, key) &&
equals(object[key], subset[key], [iterableEquality, subsetEquality]),
);
return Object.keys(subset).every(key => {
const target = subset[key];
if (isObject(target)) {
console.log(typeof target, target);
if (seenReferences.get(target)) return true;
seenReferences.set(target, true);
}

return (
object != null &&
hasOwnProperty(object, key) &&
equals(object[key], target, [
iterableEquality,
subsetEqualityWithContext(seenReferences),
])
);
});
};

return subsetEqualityWithContext(seenReferences)(object, subset);
};

export const typeEquality = (a: any, b: any) => {
Expand Down

0 comments on commit e087fc6

Please sign in to comment.