cypress: localhost URL not accessible after cy.request on server
Current behavior
Hello ๐
We are trying to integrate Cypress but we run into an issue. Cypress tells us that our frontend is not reachable. The client runs on port 4200 and the server on port 3333. We are working in an monorepo managed by NX. The client is a basic React application, and the server is a NestJS application.
First we make a call to our server using cy.request and then we use cy.visit.
The cy.visit fails (see screenshot attached) telling is that the connection is refused.
When opening http://localhost:4200 on a browser behaves as intended.
When we change the server URL to point to 127.0.0.1 things work fine (but cookies are broken, and we want cookie ๐ช).

I put together a bare bone repository where things can be reproduced: https://github.com/nicolas-marien/nx-cypress-localhost-issue
Please find attached the output of DEBUG=cypress:* NODE_DEBUG=request y nx e2e client-e2e --watch
I do not have any corporate proxy.
Thank you for your help.
Desired behavior
The cy.visit(/) call should open my client.
Test code to reproduce
Start client and server using yarn nx run-many --target=serve
Run yarn nx e2e client-e2e --watch and select the only spec file.
Cypress Version
12.3.0
Node version
18.10.0
Operating System
MacOS 12.6.1 (also happens on Ventura)
Debug Logs
[out.txt](https://github.com/cypress-io/cypress/files/10373543/out.txt)
Other
No response
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 30 (8 by maintainers)
Commits related to this issue
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Updated cypress version - Updated cypress version to address bug https://github.com/cypress-io/cypress/issues/25397 — committed to quay/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 10 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 8 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 8 months ago
- ui: Add organization and user account settings (PROJQUAY-4553) - This PR adds settings pages to the organization and user organization pages. - Admin users can edit their preferences, billing, and or... — committed to jonathankingfc/quay by jonathankingfc 8 months ago
For me instructing the bundle server (in my case vite) to explicitly use host โ127.0.0.1โ fixed the issue.
I get this error after upgrading Node from 16 to 18 or 20. Iโm not even using
cy.request().A workaround when using Vite is hinted at here: https://vitejs.dev/config/server-options.html#server-host
or set
server.hosttotruebut this will allow access to the server over the network(Adding the code
ECONNREFUSEDto this thread to make it easier to find)Iโve created a simple reproduction here. The README explains the bug in detail. My understanding of the bug is:
hosttolocalhostfor the webpack-dev-server (the default value supplied by the nrwl/webpack-dev-server executor) inhibits the use of127.0.0.1(ipv4). You can still access the frontend site vialocalhostor[::1].127.0.0.1cy.requestis executed before thecy.visit, the subsequent visit is mapped to127.0.0.1rather than::1. This causes the error as the frontend server will not accept requests from this IP as it is not listening to it.If you move the request to after the visit, everything works fine. Also, if Cypress is allowed to visit before sending the request (by commenting out the request), adding the request back in works fine. There must be some server state being set from the initial request that influences subsequent visit requests.
Can you try adding
"host": "127.0.0.1"to yourapps/client/project.jsonfor theserve.optionsconfiguration and see if it works? I tried it locally and it worked for me. You can still uselocalhostfor visiting your site locally and telling Cypress how to connect but Webpack needs to be forced to use ipv4 over ipv6 (my understanding of DNS is fuzzy admittedly).To be explicit, this is the update I made:
Still investigating if there is an issue on our side. Cypress will warn the user it canโt connect to the provided
baseUrl, but in this case there is no warning yet the connection fails when usingcy.visit.Edit: After investigating, it looks like the issue is due to Nxโs use of
localhostfor the default value ofhostused in for their webpack-dev-server executor. This use prohibits use ofipv4. Workaround described above should be enough, you can also use0.0.0.0to open both ipv4 and ipv6.@nicolas-marien I was able to reproduce your issue by using Node18. With Node16, the e2e test works fine. There was a DNS change between Node16 and Node18 (issue) that you might be running into. Iโll keep investigating to see where the problem lies and if there is a workaround for you.
We are unable to upgrade to node 18 due to this issue
My workaround for anyone interested (Using angular, but should work for any, imho)
I had a few personal requirements:
go to your support file
cypress/support/e2e.tsand add this one line of code:fetch(Cypress.env('baseUrl'), { method: 'GET' }) // fire and forgetI figured, since the cause for the issue is a cy.request() (and it was still an issue, after I replaced my login logic with fetch()), we can just do a fetch before all tests run.
Of course you can use a hardcoded url for this
fetch('http://localhost:4200', {method: 'GET'}), but I used mine from thecypress.config.tsGive me a thumbs up, if it works for you as well. I will report back, If I improve or change anything or if it stop working :C
This was solving the issue also on my dev-env. Using v12.9.0
Same issue here I was using node 18.10 and cypress 12.9.0, I downgraded the node version to 16.13.2 and that fixed the issue.