Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: toEqual() doesn't work for iterable objects if objects have symbol property
### Version

latest

### Steps to reproduce

```javascript
const Key = Symbol()
class Test {
	constructor(value) {
		this[Key] = value
	}

	[Symbol.iterator]() {
    	return [][Symbol.iterator]()
  	}
}

expect(new Test(1)).toEqual(new Test(2)) // success, because property [Key] is ignored
```

### Expected behavior

```javascript
expect(new Test(1)).toEqual(new Test(2)) // fail
```

### Actual behavior

succeed

### Additional context

Object.entries() does not return symbols, so symbols are ignored

https://github.com/facebook/jest/blob/1eb3bb5949f4ed1dbb39b9a9d9d76c3399ffd7b0/packages/expect-utils/src/utils.ts#L306-L310

### Environment

```shell
System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 16.17.0 - ~/.volta/tools/image/node/16.17.0/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.15.0 - ~/.volta/tools/image/node/16.17.0/bin/npm
  npmPackages:
    jest: latest => 29.4.3
```



Hints:
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using [StackOverflow](https://stackoverflow.com/questions/tagged/jestjs) or our [discord channel](https://discord.gg/j6FKKQQrW9) for questions.

Patch:
diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts
--- a/packages/expect-utils/src/utils.ts
+++ b/packages/expect-utils/src/utils.ts
@@ -307,8 +307,8 @@ export const iterableEquality = (
     !isImmutableOrderedSet(a) &&
     !isImmutableRecord(a)
   ) {
-    const aEntries = Object.entries(a);
-    const bEntries = Object.entries(b);
+    const aEntries = entries(a);
+    const bEntries = entries(b);
     if (!equals(aEntries, bEntries)) {
       return false;
     }
@@ -320,6 +320,15 @@ export const iterableEquality = (
   return true;
 };
 
+const entries = (obj: any) => {
+  if (!isObject(obj)) return [];
+
+  return Object.getOwnPropertySymbols(obj)
+    .filter(key => key !== Symbol.iterator)
+    .map(key => [key, obj[key]])
+    .concat(Object.entries(obj));
+};
+
 const isObject = (a: any) => a !== null && typeof a === 'object';
 
 const isObjectWithKeys = (a: any) =>


Test patch:
diff --git a/packages/expect-utils/src/__tests__/utils.test.ts b/packages/expect-utils/src/__tests__/utils.test.ts
--- a/packages/expect-utils/src/__tests__/utils.test.ts
+++ b/packages/expect-utils/src/__tests__/utils.test.ts
@@ -591,6 +591,36 @@ describe('iterableEquality', () => {
     const b = new TestRecord().set('dummy', 'data');
     expect(iterableEquality(a, b)).toBe(true);
   });
+
+  test('returns true when given a symbols keys within equal objects', () => {
+    const KEY = Symbol();
+
+    const a = {
+      [Symbol.iterator]: () => ({next: () => ({done: true})}),
+      [KEY]: [],
+    };
+    const b = {
+      [Symbol.iterator]: () => ({next: () => ({done: true})}),
+      [KEY]: [],
+    };
+
+    expect(iterableEquality(a, b)).toBe(true);
+  });
+
+  test('returns false when given a symbols keys within inequal objects', () => {
+    const KEY = Symbol();
+
+    const a = {
+      [Symbol.iterator]: () => ({next: () => ({done: true})}),
+      [KEY]: [1],
+    };
+    const b = {
+      [Symbol.iterator]: () => ({next: () => ({done: true})}),
+      [KEY]: [],
+    };
+
+    expect(iterableEquality(a, b)).toBe(false);
+  });
 });
 
 describe('typeEquality', () => {