-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Full Network Layer Stubbing / fetch support #687
Comments
@brian-mann I would be very willing to help out with this issue. This is a big blocker for use when testing our app. |
@ericadamski could you go into more detail about this? What exactly is a big blocker? |
@brian-mann making network requests (using a fetch polyfill) and asserting on those requests is important to our integration testing. Currently we are just spying on the |
You can do this right now using stubs. Albeit it's certainly not as nice as what we did for XHR stubbing, but it is possible and we have several examples of making this work. This is even in a new getting started video we're releasing later this week. |
Yes we realize it can be done, albeit we would rather wait (or work toward) a more XHR style stubbing, it is just super nice. |
Yeah it is. Okay just making sure we're on the same page. The proposal here does a decent job outlining what needs to be done but it will be a significant amount of work. Likely the next steps just need to be formalizing a plan for the API's themselves. There have been many requests for additional features and we should start there. |
That sounds good. Is it worth creating separate issues for some of the requested features for I know almost nothing about graphQL queries 😜 |
They mostly already are other issues. We need to factor in all of them and come up with a set of API's that unifies those different use cases and desired experiences. |
Right OK, I see what you are saying. |
@brian-mann side note, has it been thought of moving away from coffeescript and into es6? |
Do you consider this feature in the next release? I reckon it would be highly valuable, for instance when
|
Yeah blacklisting specific requests is part of this feature roadmap |
Add flags (query parameters) to identify and stub Mesos Operator API requests to fix the integration tests that were otherwise failing or flaky due to parsing errors in the `MesosStateStore`. The store failed to parse the `GET_MASTER` response as we could only register one stub for both the `GET_MASTER` and `SUBSCRIBE` requests resulting in JSON parse errors due to the different response formats (RecoredIO<JSON>, JSON). The flags are necessary as Cypress can only stub requests based on the URL and request method and doesn't provide the means to dynamically stub requests based on the payload as documented in cypress-io/cypress#521. We will remove the flags once the Cypress team rewrote the network handling and added support for dynamic stubs as documented in cypress-io/cypress#687 or we've addressed https://jira.mesosphere.com/browse/DCOS_OSS-2021.
Add flags (query parameters) to identify and stub Mesos Operator API requests to fix the integration tests that were otherwise failing or flaky due to parsing errors in the `MesosStateStore`. The store failed to parse the `GET_MASTER` response as we could only register one stub for both the `GET_MASTER` and `SUBSCRIBE` requests resulting in JSON parse errors due to the different response formats (RecoredIO<JSON>, JSON). The flags are necessary as Cypress can only stub requests based on the URL and request method and doesn't provide the means to dynamically stub requests based on the payload as documented in cypress-io/cypress#521. We will remove the flags once the Cypress team rewrote the network handling and added support for dynamic stubs as documented in cypress-io/cypress#687 or we've addressed https://jira.mesosphere.com/browse/DCOS_OSS-2021.
Add flags (query parameters) to identify and stub Mesos Operator API requests to fix the integration tests that were otherwise failing or flaky due to parsing errors in the `MesosStateStore`. The store failed to parse the `GET_MASTER` response as we could only register one stub for both the `GET_MASTER` and `SUBSCRIBE` requests resulting in JSON parse errors due to the different response formats (RecoredIO<JSON>, JSON). The flags are necessary as Cypress can only stub requests based on the URL and request method and doesn't provide the means to dynamically stub requests based on the payload as documented in cypress-io/cypress#521. We will remove the flags once the Cypress team rewrote the network handling and added support for dynamic stubs as documented in cypress-io/cypress#687 or we've addressed https://jira.mesosphere.com/browse/DCOS_OSS-2021.
Temporary experimental |
The You should be able to remove any of the following code regarding Cypress.on('window:before:load', (win) => {
delete win.fetch
}) or cy.visit('/', {
onBeforeLoad (win) {
delete win.fetch
},
}) And replace it with the below in your configuration file ( {
"experimentalFetchPolyfill": true
} You can see a full example of this in use here: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__window-fetch If you encounter any issues with the new |
Is it possible to stub network calls in a way that that doesn't care about whether the app uses fetch or XHR? Seems like in most cases that is an implementation detail we don't want to enshrine in a test. |
I was using Using Looks good otherwise. |
@alexkrolick We're working on bigger changes as part of #4176 to improve the experience behind network requests in general. The current workaround is meant to be helpful for users starting out so they don't have to dive through issues to figure out how to get fetch working. |
@ngbrown could you please provide a small reproducible example showing how |
cc @bahmutov
return cy
.readFile("node_modules/xhook/dist/xhook.min.js", { log: false })
.as("xhook")
.then((xhook) => {
Cypress.on("window:before:load", (win) => {
// save original fetch (which is probably Cypress's polyfill)
const originalFetch = win.fetch
win.eval(xhook)
win.fetch = originalFetch // make sure we restore `fetch` to original
win.xhook.before((req) => {
let url = req.url
if (url.indexOf("someapi") > -1 && req.body) {
// do stuff
}
})
})
}) |
So this turns out not to be as straightforward as I thought. I finally have the two working in my project but it required me to patch |
You can correlate it by hashing content length or other things like the URL & use https://developers.google.com/web/tools/chrome-devtools/network/resource-loading to have Cypress "see" in the network tab if the network request finished. To play devil's advocate, it could also be argued people should not test in this way, because a normal user would not see if a network request has finished, and tests should closely match how a user behaves (maybe waiting for some element on the page to update in response to the network request finishing instead). |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
The features requested in this issue are now possible as part of This is currently experimental and requires being enabled by passing Please see the If you encounter any issues or unexpected behavior while using |
Pull Request (in progress): #4176
What users want
window.fetch
What we do now
XMLHttpRequest
object which limits us to only stubbing XHR's from the main application frame.What we need to do
cy.server
and instead just usecy.route
blacklistDomains
option forcypress.json
and we may not be able to provide callback functions anymore.Things to consider
onRequest
andonResponse
currently provide the raw levelxhr
object. This would no longer be possible. We'd have to serialize the request / response into basic objects. No method calling would be available.onError
callback could no longer existonerror
events for XHR's and will automatically fail the test if this is called. This would no longer be possible.https
cannot be dynamic. It would have to be set incypress.json
or environment variables. The reason is that for each blacklisted domain that goes over https, we would have to route that traffic to a self signed certificate. Browser freak out if traffic to https gets assigned one certificate, and then within the same session it gets another cert applied. What this means is that before spawning the browser we have to isolate that traffic and know ahead of time not to route it to the actual server. This enables us to send a single cert to the browser.cy.route
but I'm not sure there's much of a use case here of only dynamically blacklisting non https traffic.XHR
and any other kind ofhttp
request. We'd likely have to change the page events to sayREQ
andREQ STUB
instead ofXHR
andXHR STUB
.onResponse
callback functions. It's possible for the internal server to know when an HTTP request has been completed, but it's not possible to know whether the browser has actually finished processing it. This may or may not be a problem.Bonus Points
cy.stub
, or perhaps a 3rd party extensionThe text was updated successfully, but these errors were encountered: