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

Revert "Fix circular references in iterable equality" #8259

Closed
Closed
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 @@ -8,6 +8,7 @@
### Fixes

- `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
- `[expect]` Revert "Fix circular references in iterable equality" ([#8160](https://github.com/facebook/jest/pull/8160))

### Chore & Maintenance

Expand Down
92 changes: 0 additions & 92 deletions packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
getPath,
hasOwnProperty,
subsetEquality,
iterableEquality,
} = require('../utils');

describe('getPath()', () => {
Expand Down Expand Up @@ -203,94 +202,3 @@ describe('subsetEquality()', () => {
expect(subsetEquality(undefined, {foo: 'bar'})).not.toBeTruthy();
});
});

describe('iterableEquality', () => {
test('returns true when given circular iterators', () => {
class Iter {
*[Symbol.iterator]() {
yield this;
}
}

const a = new Iter();
const b = new Iter();

expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given circular Set', () => {
const a = new Set();
a.add(a);
const b = new Set();
b.add(b);
expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given nested Sets', () => {
expect(
iterableEquality(
new Set([new Set([[1]]), new Set([[2]])]),
new Set([new Set([[2]]), new Set([[1]])]),
),
).toBe(true);
expect(
iterableEquality(
new Set([new Set([[1]]), new Set([[2]])]),
new Set([new Set([[3]]), new Set([[1]])]),
),
).toBe(false);
});

test('returns true when given circular Set shape', () => {
const a1 = new Set();
const a2 = new Set();
a1.add(a2);
a2.add(a1);
const b = new Set();
b.add(b);

expect(iterableEquality(a1, b)).toBe(true);
});

test('returns true when given circular key in Map', () => {
const a = new Map();
a.set(a, 'a');
const b = new Map();
b.set(b, 'a');

expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given nested Maps', () => {
expect(
iterableEquality(
new Map([['hello', new Map([['world', 'foobar']])]]),
new Map([['hello', new Map([['world', 'qux']])]]),
),
).toBe(false);
expect(
iterableEquality(
new Map([['hello', new Map([['world', 'foobar']])]]),
new Map([['hello', new Map([['world', 'foobar']])]]),
),
).toBe(true);
});

test('returns true when given circular key and value in Map', () => {
const a = new Map();
a.set(a, a);
const b = new Map();
b.set(b, b);

expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given circular value in Map', () => {
const a = new Map();
a.set('a', a);
const b = new Map();
b.set('a', b);

expect(iterableEquality(a, b)).toBe(true);
});
});
60 changes: 13 additions & 47 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,7 @@ const IteratorSymbol = Symbol.iterator;

const hasIterator = (object: any) =>
!!(object != null && object[IteratorSymbol]);

export const iterableEquality = (
a: any,
b: any,
aStack: Array<any> = [],
bStack: Array<any> = [],
) => {
export const iterableEquality = (a: any, b: any) => {
if (
typeof a !== 'object' ||
typeof b !== 'object' ||
Expand All @@ -158,22 +152,6 @@ export const iterableEquality = (
return false;
}

let length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
// circular references at same depth are equal
// circular reference is not equal to non-circular one
if (aStack[length] === a) {
return bStack[length] === b;
}
}
aStack.push(a);
bStack.push(b);

const iterableEqualityWithStack = (a: any, b: any) =>
iterableEquality(a, b, aStack, bStack);

if (a.size !== undefined) {
if (a.size !== b.size) {
return false;
Expand All @@ -183,7 +161,7 @@ export const iterableEquality = (
if (!b.has(aValue)) {
let has = false;
for (const bValue of b) {
const isEqual = equals(aValue, bValue, [iterableEqualityWithStack]);
const isEqual = equals(aValue, bValue, [iterableEquality]);
if (isEqual === true) {
has = true;
}
Expand All @@ -195,29 +173,25 @@ export const iterableEquality = (
}
}
}
// Remove the first value from the stack of traversed values.
aStack.pop();
bStack.pop();
return allFound;
if (allFound) {
return true;
}
} else if (isA('Map', a) || isImmutableUnorderedKeyed(a)) {
let allFound = true;
for (const aEntry of a) {
if (
!b.has(aEntry[0]) ||
!equals(aEntry[1], b.get(aEntry[0]), [iterableEqualityWithStack])
!equals(aEntry[1], b.get(aEntry[0]), [iterableEquality])
) {
let has = false;
for (const bEntry of b) {
const matchedKey = equals(aEntry[0], bEntry[0], [
iterableEqualityWithStack,
]);
const matchedKey = equals(aEntry[0], bEntry[0], [iterableEquality]);

let matchedValue = false;
if (matchedKey === true) {
matchedValue = equals(aEntry[1], bEntry[1], [
iterableEqualityWithStack,
]);
matchedValue = equals(aEntry[1], bEntry[1], [iterableEquality]);
}

if (matchedValue === true) {
has = true;
}
Expand All @@ -229,31 +203,23 @@ export const iterableEquality = (
}
}
}
// Remove the first value from the stack of traversed values.
aStack.pop();
bStack.pop();
return allFound;
if (allFound) {
return true;
}
}
}

const bIterator = b[IteratorSymbol]();

for (const aValue of a) {
const nextB = bIterator.next();
if (
nextB.done ||
!equals(aValue, nextB.value, [iterableEqualityWithStack])
) {
if (nextB.done || !equals(aValue, nextB.value, [iterableEquality])) {
return false;
}
}
if (!bIterator.next().done) {
return false;
}

// Remove the first value from the stack of traversed values.
aStack.pop();
bStack.pop();
return true;
};

Expand Down