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

automatically polyfill window.fetch with XMLHttpRequest #7597

Merged
merged 32 commits into from
Jun 15, 2020

Conversation

bahmutov
Copy link
Contributor

@bahmutov bahmutov commented Jun 5, 2020

User facing changelog

Cypress will automatically replace window.fetch with polyfill that drops down to XMLHttpRequest object that can be intercepted using built-in Network API

Additional details

  • polyfill using https://github.com/developit/unfetch that is small and has essential fetch methods
  • testing against pull request in recipes where I have removed the existing polyfills
# in the local recipes repo ~/git/cypress-example-recipes
git checkout fetch-polyfill
cd examples/stubbing-spying__window-fetch
npm start

# run this repo run against that recipe
yarn dev --project ~/git/cypress-example-recipes/examples/stubbing-spying__window-fetch

How has the user experience changed?

Before if the web application used fetch, the user had to set up polyfill / delete window.fetch on each window load in order to use our network features. With this feature, the user can simply set in cypress.json experimentalFetchPolyfill: true and Cypress automatically polyfills window.fetch via XMLHttpRequest which we stub

Open questions

  • do we need an experimental flag? I am afraid that unfetch might be different from the standard fetch behavior. For example, it does not support streaming responses... If we automatically polyfill native fetch, web apps that do not even need network support suddenly would stop working inside Cypress. Answer: Yes

PR Tasks

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 5, 2020

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

@cypress
Copy link

cypress bot commented Jun 5, 2020



Test summary

7588 0 119 0


Run details

Project cypress
Status Passed
Commit 05e89b1
Started Jun 15, 2020 2:56 PM
Ended Jun 15, 2020 3:03 PM
Duration 07:04 💡
OS Linux Debian - 10.1
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

Copy link
Contributor

@JessicaSachs JessicaSachs left a comment

Choose a reason for hiding this comment

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

This is super succinct. I read through unfetch. Nice find!

I have one nit which is that I'd really love to avoid toString + eval. I wrote notes on how we might use the proper XMLHttpRequest object. LMK if that makes sense.

// drop "fetch" polyfill that replaces it with XMLHttpRequest
// by using "eval" we force the polyfill to use XMLHttpRequest objects
// from the app iframe that we wrap for network stubbing
contentWindow.eval(`fetch=${fetchPolyfill}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can pretty easily avoid eval + toString and use the correct XMLHttpRequest object if we patch-package and take in the XMLHttpRequest (or content window) reference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ughh, but that would be more complex to set up and maintain. Remember the has-binary2 disaster

@bahmutov
Copy link
Contributor Author

bahmutov commented Jun 9, 2020

Note: the webpack bundles the following file

var fetchPolyfill = __webpack_require__("../driver/node_modules/unfetch/dist/unfetch.mjs")["default"];

which in the unfetch package.json file is listed under module and jsnext:main

packages/driver/README.md Outdated Show resolved Hide resolved
packages/driver/README.md Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@bahmutov bahmutov requested a review from flotwig June 11, 2020 13:54
@bahmutov bahmutov marked this pull request as ready for review June 12, 2020 13:56
JessicaSachs
JessicaSachs previously approved these changes Jun 12, 2020
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.

polyfill window.fetch
3 participants