Closed
Description
Many developers are migrating to window.fetch
from XHRs due to the simplified async interface it exposes. I happen to be one of those developers.
Cypress should support window.fetch
with similar abilities to those of XHRs: I should be able to query the request and response, wait on them, and mock with data and fixtures.
Cypress team edit
This feature will work seamlessly once Full network stubbing is implemented.
Workarounds
- use experimental fetch polyfill available in v4.9.0
or do it yourself:
- delete
window.fetch
before visiting the page to force your application to polyfill it on top of XHR. If the application does not include a polyfill, you can load one from your tests. - stub
window.fetch
directly in JavaScript
See these solutions in Stubbing window.fetch
example recipe
Activity
brian-mann commentedon Aug 31, 2016
Per the
0.17.0
release at the very leastwindow.fetch
will actually work properly now.It's on our radar to adding stubbing to these as well. We're a lot closer to being able to do it now.
PhilippSpo commentedon Nov 30, 2016
I would love to see this feature. I am very excited about stubbing. It will definitely solve a lot of problems when doing these tests.
window.fetch
is very widely used by now 👍thomas-huston-zocdoc commentedon Dec 7, 2016
My team would love this too!
window.fetch
is very commonly used in React applications now, so I'm sure other teams would appreciate this as well.nwehrle commentedon Jan 16, 2017
Same here. We use fetch for it's simplicity in our react application. Until yet we use our own proxy that mocks the ajax requests, but your XHR approuch is much more readable.
nwehrle commentedon Jan 16, 2017
Maybe you could offer a way to use fetch outside the service-worker's context first and in a second phase you could add fetch in service-workers?
brian-mann commentedon Feb 11, 2017
There is now an example of using
cy.stub
to controlwindow.fetch
. It's not the same API ascy.route
but it will work today!We have a bunch of examples of it here: https://github.com/cypress-io/cypress-example-recipes#controlling-behavior-with-spies-stubs-and-clocks
stigkj commentedon Feb 21, 2017
As not all browsers support
window.fetch
yet, I guess most use some form of polyfill for this. Then one could do something like this:After doing this, the polyfill will just use
XHR
instead offetch
in the test, and one can use the normal Cypress's capturing/mocking tools. What do you think of this, @brian-mann?nwehrle commentedon Feb 28, 2017
Thank you for the alternative approach. Will try that.
What is the state of the real implementation? https://github.com/cypress-io/cypress/projects/20
mvandebunt commentedon Mar 6, 2017
@stigkj thx for the workaround/solution.
the solution in https://github.com/cypress-io/cypress-example-recipes#controlling-behavior-with-spies-stubs-and-clocks was working for several projects for me.
Now I need to test an app using redux-saga with
yield call(POST, { url: '/api/login/users', options });
and there it is not working, your solution is working!mukaibot commentedon Mar 28, 2017
@stigkj thanks for the work around, like @mvandebunt this has worked for me in Chromium 56 :)
I find this a bit easier than the spy approach. I'm looking forward to Cypress getting better support for window.fetch :)
161 remaining items
bengry commentedon Jul 15, 2020
Same here. We use a
POST
request to make a login, passing in the username and password in the request's body as a JSON object. However, using the experimental polyfill the request headers are changed, and theContent-Type
is nowtext/plain;charset=UTF-8
, which our backend server rejects.I think this happens because we utilize the
Headers
class in ourfetch
call, and don't pass a plain object (e.g.{ 'Content-Type': 'application/json' }
). We do polyfillHeaders
(usingfetch-headers
), but seems like the polyfill used byexperimentalFetchPolyfill
(unfetch
) doesn't support them even when they're polyfilled.This workaround (in our app code) works, although slightly awkward:
baeharam commentedon Jul 23, 2020
Is it valid to use fetch polyfill called
whatwg-fetch
to capturefetch
?mefechoel commentedon Jul 23, 2020
@baeharam I did this for a project and it worked just fine.
I just ran into a Problem with very large response payloads.
If the response was bigger tham ~1mb the request failed.
Thats not a problem with the polyfill though, but rather with the way cypress mocks xhr requests.
baeharam commentedon Jul 24, 2020
@mefechoel Thanks for your feedback! I'll try
whatwg-fetch
for my graphql query 😄cbstauss commentedon Jul 31, 2020
Is there a way to only replace window.fetch on stubbed routes?
cypress-bot commentedon Sep 1, 2020
Released in
5.1.0
.This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.1.0, please open a new issue.
jennifer-shehane commentedon Oct 21, 2020
The features requested in this issue are now possible as part of
cy.route2()
.cy.route2()
is currently experimental and requires being enabled by passing"experimentalNetworkStubbing": true
through your Cypress configuration. This will eventually be merged in as part of our standard API.Please see the
cy.route2()
docs for full details: https://on.cypress.io/route2If you encounter any issues or unexpected behavior while using
cy.route2()
we encourage you to open a new issue so that we can work out all the issues before public release. Thanks!