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

Iterable equality within object #8359

Merged
merged 15 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 43 additions & 0 deletions packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,49 @@ describe('iterableEquality', () => {
).toBe(false);
});

test('returns true when given iterator within equal objects', () => {
const a = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: [],
};
const b = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: [],
};

expect(iterableEquality(a, b)).toEqual(true);
jeysal marked this conversation as resolved.
Show resolved Hide resolved
});

test('returns false when given iterator within inequal objects', () => {
const a = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: [1],
};
const b = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: [],
};

expect(iterableEquality(a, b)).toEqual(false);
});

test('returns false when given iterator within inequal nested objects', () => {
const a = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: {
b: [1],
},
};
const b = {
[Symbol.iterator]: () => ({next: () => ({done: true})}),
a: {
b: [],
},
};

expect(iterableEquality(a, b)).toEqual(false);
});

test('returns true when given circular Set shape', () => {
const a1 = new Set();
const a2 = new Set();
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ export const iterableEquality = (
return false;
}

if (!emptyObject(a) && !emptyObject(b)) {
jeysal marked this conversation as resolved.
Show resolved Hide resolved
const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (!equals(aEntries, bEntries)) {
return false;
}
}

// Remove the first value from the stack of traversed values.
aStack.pop();
bStack.pop();
Expand Down