Repository: jestjs/jest (TypeScript)

Issue:
[Bug]: Native fetch Arrays Incorrectly Matched
### Version

v30.0.0-alpha.3 and v29.7.0

### Steps to reproduce

1. Create a test file with this code
```js
it('matches fetch parsed Array', async () => {
    expect(await (await fetch('https://avatar.roblox.com/v1/users/1/outfits')).json()).toMatchObject({
        data: expect.any(Array),
        filteredCount: expect.any(Number),
        total: expect.any(Number)
    })
})

it('matches manually parsed Array', async () => {
    expect(JSON.parse(await (await fetch('https://avatar.roblox.com/v1/users/1/outfits')).text())).toMatchObject({
        data: expect.any(Array),
        filteredCount: expect.any(Number),
        total: expect.any(Number)
    })
})
```

2. Run Jest 

### Expected behavior

Jest should identify the data property in the response body parsed by fetch as an Array.

### Actual behavior

Jest does not identify the data property in the response body as an Array if it is parsed by fetch, only if it is parsed using JSON.parse.

<img width="356" alt="Screenshot 2024-04-12 at 8 37 47 AM" src="https://github.com/jestjs/jest/assets/29686338/ee6c9d93-8fec-4e10-831a-20812f1fa279">
<img width="338" alt="Screenshot 2024-04-12 at 8 37 30 AM" src="https://github.com/jestjs/jest/assets/29686338/a347f42c-74a2-4e07-8422-6c0d5944f9a2">


### Additional context

I am using the latest Node.js LTS version (v20.12.2)

### Environment

```shell
System:
    OS: macOS 13.2.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.12.2 - /usr/local/bin/node
    npm: 10.5.0 - /usr/local/bin/npm
    pnpm: 7.30.3 - /usr/local/bin/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
```

Fix Array matching
Fixes matching type Array for arrays created in a new context.

## Summary

Fixes #15025
Jest does not correctly match arrays created in different contexts. This causes it to incorrectly match arrays from HTTP requests made by native fetch, failing tests when they should be passing.

## Test plan

Added a unit test to asymmetricMatchers for arrays created in a new context.

Before:
![image](https://github.com/jestjs/jest/assets/29686338/dad491c0-79f7-49d6-b72e-00beeae14baf)

After:
![image](https://github.com/jestjs/jest/assets/29686338/18fc67a4-8f9e-4527-9a12-b15d3b9add64)



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.
Still an issue.
This is https://github.com/jestjs/jest/issues/2549

You can probably use `Array.from` in your test code, that might help. Not sure there's much else we can do 🤔

We could fix the array case explicitly probably. `Array.isArray` works across realms
Happy to take a PR fixing this for arrays
> Happy to take a PR fixing this for arrays

Where in the code is the array matching? I attempted to look for it and fix the issue but the codebase is way too complex.
https://github.com/jestjs/jest/blob/7bffeb55a337cb2b3e5ff019cc0754be8dcf5883/packages/expect/src/asymmetricMatchers.ts#L110

Add a new `if (this.sample == Array) return Array.isArray(other)`. And maybe in `getExpectedType()`?
 <a href="https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/35323119/15062869/15091/#/?version=2"><img src="https://s3.amazonaws.com/cla-project-logo-prod/cla-not-signed.svg" alt="CLA Not Signed" align="left" height="28" width="328"></a><br/><br /><ul><li><a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/35323119/15062869/15091/#/?version=2' target='_blank'>:x:</a> - login: @popeeyy / name: popeeyy . The commit (b791c90377f3941bf5a51364aa93ed341380f146, 7425a1517182b8db8618b23f270082051dca6135, c4e5710d153f585fd1c23b5a3236c10aab4093e2) is not authorized under a signed CLA. <a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/35323119/15062869/15091/#/?version=2' target='_blank'>Please click here to be authorized</a>. For further assistance with EasyCLA, <a href='https://jira.linuxfoundation.org/servicedesk/customer/portal/4' target='_blank'>please submit a support request ticket</a>.</li></ul><!-- Date Modified: 2024-05-27 21:52:40.491893 -->
### <span aria-hidden="true">✅</span> Deploy Preview for *jestjs* ready!
Built [without sensitive environment variables](https://docs.netlify.com/configure-builds/environment-variables/#sensitive-variable-policy)

|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | c4e5710d153f585fd1c23b5a3236c10aab4093e2 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/jestjs/deploys/665500a724c924000872565b |
|<span aria-hidden="true">😎</span> Deploy Preview | [https://deploy-preview-15091--jestjs.netlify.app](https://deploy-preview-15091--jestjs.netlify.app) |
|<span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />![QR Code](https://app.netlify.com/qr-code/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJodHRwczovL2RlcGxveS1wcmV2aWV3LTE1MDkxLS1qZXN0anMubmV0bGlmeS5hcHAifQ._GtRsnqzbL4pQA2AeGV1kyXZobQyMYzJwBwlVUDZfhg)<br /><br />_Use your smartphone camera to open QR code link._</details> |
---

_To edit notification comments on pull requests, go to your [Netlify site configuration](https://app.netlify.com/sites/jestjs/configuration/deploys#deploy-notifications)._
Oh, can you sign the CLA?
> Oh, can you sign the CLA?

No.
That's unfortunate, I don't think we'll be able to merge the PR in that case 🤔 https://github.com/jestjs/jest/blob/b7ae0b85e981eae0e373d98ab2ae8fe497d90027/CONTRIBUTING.md#contributor-license-agreement-cla

>In order to accept your pull request, we need you to submit a CLA.
> That's unfortunate, I don't think we'll be able to merge the PR in that case 🤔 https://github.com/jestjs/jest/blob/b7ae0b85e981eae0e373d98ab2ae8fe497d90027/CONTRIBUTING.md#contributor-license-agreement-cla
> 
> 
> 
> >In order to accept your pull request, we need you to submit a CLA.

That is quite unfortunate, but I will not be sharing my full name, address, and all the other information you should not share with random organizations to contribute a few lines of code. Hopefully someone else willing to give this information away will be able to fix this issue!

Patch:
diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts
--- a/packages/expect/src/asymmetricMatchers.ts
+++ b/packages/expect/src/asymmetricMatchers.ts
@@ -136,6 +136,10 @@ class Any extends AsymmetricMatcher<any> {
       return typeof other == 'object';
     }
 
+    if (this.sample == Array) {
+      return Array.isArray(other);
+    }
+
     return other instanceof this.sample;
   }
 
@@ -164,6 +168,10 @@ class Any extends AsymmetricMatcher<any> {
       return 'boolean';
     }
 
+    if (Array.isArray(this.sample)) {
+      return 'array';
+    }
+
     return fnNameFor(this.sample);
   }
 


Test patch:
diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts
--- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts
+++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts
@@ -6,6 +6,7 @@
  *
  */
 
+import {runInNewContext} from 'node:vm';
 import jestExpect from '../';
 import {
   any,
@@ -51,6 +52,7 @@ test('Any.asymmetricMatch() on primitive wrapper classes', () => {
     any(Boolean).asymmetricMatch(new Boolean(true)),
     any(BigInt).asymmetricMatch(Object(1n)),
     any(Symbol).asymmetricMatch(Object(Symbol())),
+    any(Array).asymmetricMatch(runInNewContext('[];')),
     /* eslint-enable */
   ]) {
     jestExpect(test).toBe(true);