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 1 commit
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
13 changes: 13 additions & 0 deletions packages/expect/src/__tests__/utils.test.js
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
7 changes: 5 additions & 2 deletions packages/expect/src/utils.ts
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,8 +254,8 @@ export const iterableEquality = (
return false;
}

const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
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;
}
Expand Down