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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299))
- `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335))
- `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352))
- `[expect]` Fix `iterableEquality` ignores other properties ([#8359](https://github.com/facebook/jest/pull/8359))

### Chore & Maintenance

Expand Down
56 changes: 56 additions & 0 deletions packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {stringify} = require('jest-matcher-utils');
const {
emptyObject,
getObjectSubset,
getObjectEntries,
getPath,
hasOwnProperty,
subsetEquality,
Expand Down Expand Up @@ -166,6 +167,18 @@ describe('getObjectSubset()', () => {
});
});

describe('getObjectEntries', () => {
test('returns an empty array when called with null', () => {
expect(getObjectEntries(null)).toEqual([]);
});
test('returns an empty array when called with undefined', () => {
expect(getObjectEntries(undefined)).toEqual([]);
});
test('returns object entries', () => {
expect(getObjectEntries({ a: 1, b: 2 })).toEqual([['a', 1], ['b', 2]]);
});
})

describe('emptyObject()', () => {
test('matches an empty object', () => {
expect(emptyObject({})).toBe(true);
Expand Down Expand Up @@ -271,6 +284,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
9 changes: 9 additions & 0 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export const getObjectSubset = (object: any, subset: any): any => {
return object;
};

export const getObjectEntries = (object: any): Array<any> =>
d7my11 marked this conversation as resolved.
Show resolved Hide resolved
object == null ? [] : Object.keys(object).map((key) => [key, object[key]])

const IteratorSymbol = Symbol.iterator;

const hasIterator = (object: any) =>
Expand Down Expand Up @@ -251,6 +254,12 @@ export const iterableEquality = (
return false;
}

const aEntries = getObjectEntries(a);
const bEntries = getObjectEntries(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