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(expect-utils): Circular references not being caught when inside arrays #14894

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670))
- `[@jest/expect-utils]` Fix comparison of `URL` ([#14672](https://github.com/jestjs/jest/pull/14672))
- `[@jest/expect-utils]` Check `Symbol` properties in equality ([#14688](https://github.com/jestjs/jest/pull/14688))
- `[@jest/expect-utils]` Catch circular references within arrays when matching objects ([#14894](https://github.com/jestjs/jest/pull/14894))
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
- `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589))
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
Expand Down
89 changes: 89 additions & 0 deletions e2e/__tests__/circularRefInBuiltInObj.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

describe('matches circular references nested in:', () => {
interface CircularObj {
ref: unknown;
[prop: string]: unknown;
}

test('arrays', () => {
type CircularArray = CircularObj & {ref: Array<unknown>};

const a: CircularArray = {c: 1, ref: [1]};
const b: CircularArray = {c: 1, ref: [1]};

a.ref.push(a);
b.ref.push(b);
expect(a).toMatchObject(b);

b.ref = [];
expect(a).not.toMatchObject(b);

b.ref = [1];
expect(a).not.toMatchObject(b);
});

test('deeply nested array properties', () => {
type DeepCircularArray = CircularObj & {ref: {inner: Array<unknown>}};
const a: DeepCircularArray = {
c: 1,
ref: {
inner: [1],
},
};
const b: DeepCircularArray = {
c: 1,
ref: {
inner: [1],
},
};
a.ref.inner.push(a);
b.ref.inner.push(b);
expect(a).toMatchObject(b);

b.ref.inner = [];
expect(a).not.toMatchObject(b);

b.ref.inner = [1];
expect(a).not.toMatchObject(b);
});

test('sets', () => {
type CircularSet = CircularObj & {ref: Set<unknown>};

const a: CircularSet = {c: 1, ref: new Set()};
const b: CircularSet = {c: 1, ref: new Set()};

a.ref.add(a);
b.ref.add(b);
expect(a).toMatchObject(b);

b.ref.clear();
expect(a).not.toMatchObject(b);

b.ref.add(1);
expect(a).not.toMatchObject(b);
});

test('maps', () => {
type CircularMap = CircularObj & {ref: Map<string, unknown>};

const a: CircularMap = {c: 1, ref: new Map()};
const b: CircularMap = {c: 1, ref: new Map()};

a.ref.set('innerRef', a);
b.ref.set('innerRef', b);
expect(a).toMatchObject(b);

b.ref.clear();
expect(a).not.toMatchObject(b);

b.ref.set('innerRef', 1);
expect(a).not.toMatchObject(b);
});
});
8 changes: 6 additions & 2 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,14 @@ export const subsetEquality = (
return undefined;
}

return getObjectKeys(subset).every(key => {
if (seenReferences.has(subset)) return undefined;
seenReferences.set(subset, true);

const matchResult = getObjectKeys(subset).every(key => {
if (isObjectWithKeys(subset[key])) {
if (seenReferences.has(subset[key])) {
return equals(object[key], subset[key], filteredCustomTesters);
}
seenReferences.set(subset[key], true);
}
const result =
object != null &&
Expand All @@ -377,6 +379,8 @@ export const subsetEquality = (
seenReferences.delete(subset[key]);
return result;
});
seenReferences.delete(subset);
return matchResult;
};

return subsetEqualityWithContext()(object, subset);
Expand Down
Loading