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

Add fetch to global scope during Jest setup #30488

Closed
wants to merge 4 commits into from

Conversation

dstaley
Copy link

@dstaley dstaley commented Nov 27, 2020

Summary

In the Jest documentation, the react-native preset in this repo is described as follows:

The preset is a node environment that mimics the environment of a React Native app.

However, while writing unit tests, I noticed that the Jest environment is missing a fetch implementation. According to the React Native docs, React Native includes a polyfill for fetch. Putting these two statements together, it follows that there should be an implementation of fetch present in the Jest environment while testing.

This PR adds a single line, jest.requireActual('../Libraries/Network/fetch'), to the Jest setup script. (As an alternative, I believe I could just use require('whatwg-fetch'), so if that's preferred please let me know and I'll update the PR.)

Changelog

[General] [Fixed] - Added fetch to global scope during Jest tests

Test Plan

I'm not sure if this change would require any updates to existing tests. If that's not the case, please let me know! As far as I can tell, there aren't any tests specific to the Jest preset.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2020
Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you explain why you had to update whatwg-fetch? I'd prefer to just make the code change to the Jest setup in this PR if possible.

@dstaley
Copy link
Author

dstaley commented Dec 1, 2020

Thanks! Can you explain why you had to update whatwg-fetch? I'd prefer to just make the code change to the Jest setup in this PR if possible.

v3 referenced self which is undefined in Node environments. This was fixed in v3.3, but I figured since all the tests passed with v3.5 that I might as well go all the way. There's more details about the changes here.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@fabOnReact
Copy link
Contributor

this is awesome thanks 🙏

@daniel-nagy
Copy link
Contributor

It seems Libraries/Core/setUpXHR.js needs to be loaded for fetch to work properly. For example, fetch requires the AbortController and AbortSignal APIs.

@github-actions
Copy link

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Sep 23, 2023
@github-actions
Copy link

This PR was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants