Skip to content

Commit

Permalink
Iterable equality within object (#8359)
Browse files Browse the repository at this point in the history
  • Loading branch information
d7my11 committed Feb 24, 2022
1 parent 103fb15 commit 536efe8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -39,6 +39,7 @@

- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
- `[expect]` Throw useful error if `expect.extend` is called with invalid matchers ([#12488](https://github.com/facebook/jest/pull/12488))
- `[expect]` Fix `iterableEquality` ignores other properties ([#8359](https://github.com/facebook/jest/pull/8359))
- `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378))
- `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397))
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
Expand Down
43 changes: 43 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Expand Up @@ -422,6 +422,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-utils/src/utils.ts
Expand Up @@ -254,6 +254,12 @@ export const iterableEquality = (
return false;
}

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

0 comments on commit 536efe8

Please sign in to comment.