Intercepting server-side requests for SSR (global-agent proxy via Cypress network stubbing?) #25298
Replies: 2 comments
-
I have a similar issue too in nuxt. |
Beta Was this translation helpful? Give feedback.
-
What about just starting a small express server instead? My Nuxt Middleware
So in order to make cypress tests work, I need to mock that backend api request, if I won't work with the correct backend. After many hours of searching, I just think of, what about a simple express server, which "mocks" the request
Keep in mind, the express server should have the same URL as the server or configure your app to use it via env...
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I haven't seen much information on how to use Cypress for writing functional tests for SSR applications and wanted to see if what I've done seems reasonable.
For context, I'm using the new Qwik framework and wanted to add tests where I mock out the API responses. I want Qwik to use the
cy.intercept
as part of the server-side rendering step rather than making any real API calls.After digging into Cypress network stubbing a little bit, I tested to see if I could just proxy the server
node-fetch
calls (viaglobal-agent
) to the Cypress server and it surprisingly all worked!I'm running the Qwik server with:
GLOBAL_AGENT_HTTP_PROXY=http://localhost:12345 NODE_TLS_REJECT_UNAUTHORIZED=0 node -r 'global-agent/bootstrap' server/entry.express
where
12345
is the fixed port the Cypress server will bind to (defined incypress.config.ts
).And if I set up the interceptors before visiting the page, like this:
the Qwik server does end up receiving the fixture in its
node-fetch
calls!With this all said and done though, I wanted to see if anyone else has tried something like this before and/or if there are any better approaches here.
Beta Was this translation helpful? Give feedback.
All reactions