| Name | Version |
| ------- | ------- |
| msw | ^0.20.5 |
| browser | Chrome 85.0.4183.102 (Official Build) (64-bit) (cohort: Stable) |
| OS | Windows 10 |
I have request handlers set up correct because they work both in regular browser, in cypress with http reques triggered from browser and in the jest tests
I use mobx store for my apps global state, I have some util methods to apply some snapshot to test specific cases. I added corresponding endpoints to apply those snapshots.
They works when they are triggered by the app, but not triggered when I call them with cy.request, I made sure it's called after the msw is initialized.
To msw to give back response as if it would be created from the app itself.
Hi, @hakankaraduman đź‘‹
Let’s take a look how cy.request() works under the hood. The investigation we are about to perform is relevant for [email protected].
When cy.request() is called, it returns a Promise of Cypress.backend() call:
Fast-forwarding to where .backend() method is defined:
We can see that it creates a Promise (returned from cy.request()) and emits the ”backend:request" event. By searching the Cypress repository for that event we find this reference in the event manager:
That event is listed as the one to be transported to the socket. I don’t have any knowledge on Cypress internals, so if this is unknown to you, that’s unknown to me as well. However, let’s see what driverToSocketEvents affects.
Here we see that they iterate over each such event and create an event listener for each. In the listener they propagate that event to the ws instance via ws.emit().
And here’s the declaration for the ws instance:
Diving further down, that’s how the “backend:request” event is handled by the web socket:
I can assume that in case of HTTP request this switch case is what gets executed:
There are two conclusions we can draw from this lengthy codebase journey:
cy.request(), but rather treats it as an event emitter, signaling to the web socket server to handle that event.cy.request().Based on those conclusions I’m afraid there’s nothing MSW could (or should) do with this behavior. The request you make with cy.request() doesn’t leave your app as an HTTP request, but rather as a WebSocket event. We do not support Web Sockets yet, however, the implementation is in works (see #156). Bear in mind that even when we do support sockets, that won’t help in this case, as handling an HTTP request via Web Socket API is misleading at best. You shouldn’t go that deep into Cypress implementation detail to mock a request.
I highly recommend to perform the request as a usual request in your app. That means either via fetch, XMLHttpRequest, or any third-party request issuing libraries to your liking (axios, react-query, etc.). That would result into an actual HTTP request, and such request can be intercepted and mocked by MSW.
Hope this helps.
Hi @kettanaito
Thank you so much for this detailed explanation. I learned a lot and understood how to structure my tests for cypress. You're great.