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

[Bug]: Native fetch Arrays Incorrectly Matched #15025

Closed
popeeyy opened this issue Apr 12, 2024 · 8 comments · Fixed by #15101
Closed

[Bug]: Native fetch Arrays Incorrectly Matched #15025

popeeyy opened this issue Apr 12, 2024 · 8 comments · Fixed by #15101
Labels

Comments

@popeeyy
Copy link

popeeyy commented Apr 12, 2024

Version

v30.0.0-alpha.3 and v29.7.0

Steps to reproduce

  1. Create a test file with this code
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)
    })
})
  1. 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.

Screenshot 2024-04-12 at 8 37 47 AM Screenshot 2024-04-12 at 8 37 30 AM

Additional context

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

Environment

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
Copy link

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.

@github-actions github-actions bot added the Stale label May 12, 2024
@popeeyy
Copy link
Author

popeeyy commented May 12, 2024

Still an issue.

@SimenB
Copy link
Member

SimenB commented May 12, 2024

This is #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

@popeeyy popeeyy closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2024
@SimenB
Copy link
Member

SimenB commented May 12, 2024

Happy to take a PR fixing this for arrays

@popeeyy
Copy link
Author

popeeyy commented May 12, 2024

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.

@SimenB
Copy link
Member

SimenB commented May 12, 2024

asymmetricMatch(other: unknown) {

Add a new if (this.sample == Array) return Array.isArray(other). And maybe in getExpectedType()?

Copy link

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 or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2024
@SimenB
Copy link
Member

SimenB commented Aug 8, 2024

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants