Skip to content

Conversation

@alexandre-abrioux
Copy link

@alexandre-abrioux alexandre-abrioux commented Nov 7, 2025

Motivation and Resolution

When running tests with Jest using testEnvironment: "jsdom" to simulate a browser environment, globalThis is defined but globalThis.fetch is removed by Jest.

When using Starknet's Javascript library, we end up with the following error in tests:

TypeError: Cannot read properties of undefined (reading 'bind')

This is a fix to remediate that issue.

Usage related changes

N/A

Development related changes

N/A

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Linked the issues which this PR resolves
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing

@tabaktoni
Copy link
Member

This would produce an issue if globalThis, window, global are unreferenced.

typeof a?.fetch

VM6129:1 Uncaught ReferenceError: a is not defined
    at <anonymous>:1:1

On the other hand, for unreferenced elements, this is fine

typeof a

'undefined'

fetch is a known issue with jsdom, and is not the best solution for testing. There are multiple issues and references to using a headless browser rather than it.
But in this case, you can also set this at the start of the code execution:
config.set('fetch', yourfetch);

@tabaktoni tabaktoni requested a review from penovicp November 14, 2025 09:42
@alexandre-abrioux
Copy link
Author

alexandre-abrioux commented Nov 14, 2025

@tabaktoni Thanks for reviewing this PR! And I apologize for my mistake; I should have tested this further.

I agree that jsdom is not the best solution, and ideally, we should use a headless browser. Unfortunately, I'm currently unable to do that on the project I'm working on, but I've already applied a quick fix, similar to the one you suggested.

The goal of this PR was to contribute a general fix that would prevent it from happening to someone else. I updated the condition, and it should now work for all cases. Let me know what you think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants