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 13 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master

- `[expect]` Fix `iterableEquality` ignores other properties ([#8359](https://github.com/facebook/jest/pull/8359))
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### Features

- `[babel-plugin-jest-hoist]` Show codeframe on static hoisting issues ([#8865](https://github.com/facebook/jest/pull/8865))
Expand Down
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 @@ -434,6 +434,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)).toBe(true);
});

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)).toBe(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)).toBe(false);
});

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

const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (!equals(aEntries, bEntries)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add [iterableEquality] as optional customTesters argument to equals call in case any of these property values have iterators? All the calls in matchers.ts include it. I am asking, not telling.

This illustrates a limitation that we recently noticed: it is not (yet) possible to for a custom tester to know about sibling testers (for example, toStrictEqual matcher needs more testers than toEqual does, and iterableEquality has no way to provide them in this recursive call).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrottimark is there an extra test case you're thinking of we should add?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so sorry for the very late follow up.
I'm not familiar with the code base but I see the point and the limitation.

This illustrates a limitation that we recently noticed: it is not (yet) possible to for a custom tester to know about sibling testers

IMO customTesters should be kept like this so each custom tester should test only one thing, so my changes in iterableEquality method will be reverted and will include non-enumerable members again in keys method in jasmineUtils.ts
I'm not sure of the consequences of this change but will try it out.

return false;
}

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