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 🍪).

Capture d’écran 2023-01-09 à 11 49 55

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 4 years ago
  • Reactions: 9
  • Comments: 35 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve created a simple reproduction here. The README explains the bug in detail. My understanding of the bug is:

  • Setting host to localhost for the webpack-dev-server (the default value supplied by the nrwl/webpack-dev-server executor) inhibits the use of 127.0.0.1 (ipv4). You can still access the frontend site via localhost or [::1].
  • The backend server is listening on 127.0.0.1
  • When the cy.request is executed before the cy.visit, the subsequent visit is mapped to 127.0.0.1 rather 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 your apps/client/project.json for the serve.options configuration and see if it works? I tried it locally and it worked for me. You can still use localhost for 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:

"serve": {
  "executor": "@nrwl/webpack:dev-server",
  "defaultConfiguration": "development",
  "options": {
    "buildTarget": "client:build",
    "hmr": true,
    "host": "127.0.0.1" <----
  },

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 using cy.visit.

Edit: After investigating, it looks like the issue is due to Nx’s use of localhost for the default value of host used in for their webpack-dev-server executor. This use prohibits use of ipv4. Workaround described above should be enough, you can also use 0.0.0.0 to open both ipv4 and ipv6.

For me instructing the bundle server (in my case vite) to explicitly use host ‘127.0.0.1’ fixed the issue.

My workaround for anyone interested (Using angular, but should work for any, imho)

I had a few personal requirements:

  • I did not want to change the host property. I want the app to run on localhost, not on 127.0.0.1
  • The workaround should not affect performance (total runtime of e2e test) to much (not at all at best)
  • should not cause alot of boilerplate
  • should not require thrid party tools, any extra effort

go to your support file cypress/support/e2e.ts and add this one line of code: fetch(Cypress.env('baseUrl'), { method: 'GET' }) // fire and forget

Edit 2024-08-29: The intend is to have the first http request go the front end. The support file runs before the tests (to eliminate race conditions). Depending on what other code you have in your support file, you should most likely put that line of code at the top of the file.

I 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 the cypress.config.ts image

Give 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

@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.

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

// vite.config.js
import { defineConfig } from 'vite'
import dns from 'dns'

dns.setDefaultResultOrder('ipv4first')

export default defineConfig({ ...

or set server.host to true but this will allow access to the server over the network


(Adding the code ECONNREFUSED to this thread to make it easier to find)

We are unable to upgrade to node 18 due to this issue

News about this? We are using Gatsby v5, and Node 18 is a requirement.

Things I’ve tried:

  • setting Node v20
  • NODE_OPTIONS: --dns-result-order=ipv4first
  • using 127.0.0.1 as baseURL instead of localhost

Considering this bug is almost a year old, I’d like to know if there is any workaround I’m missing.

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.

@atefevan

The solution is insane ! Works on Node 18 as well as 20 !! Could you explain the solution ?

Here is the original comment: https://github.com/cypress-io/cypress/issues/25397#issuecomment-2030468503

It was quite some time ago. To explain it propperly I’d need dive into the cypress code itself. The bug is not really transparent and I don’t wanna invest the time to dig deep into it.

Please note the solution is a hacky work-around and not a good solution. (Yet, it didnt seem to cause any other harm so might as well use the quick hack and continue productivity)

Explanation to what I know: When I refer to http request or just requests I mean ´cy.request()´ ´cy.visit()´ etc… they all just http requests.
The bug occurs when the first request is not your front end. In this case your other requests will fail (If I remeber correctly, this includes the requests being made by cypress itself).

Example:
Imagine you have two tests run in parallel. One test (lets call it “SERVER”-test) starts of with a cy.request to the server, whereas the other “CLIENT”-test starts of with a front end cy.visist(). You now have a race condition. You don’t know which one will run first. Assumingly both tests are correct, they will both fail if the “SERVER”-test runs first. Likewise both will succeed if the “CLIENT”-tests runs first.

To prevent that we simply make sure that the very first request is made to the front end. We do this by putting that one line of code into the support file. It can by a cy.visit, a cy.request a fetch, or whatever http request (any will work). (There might be a better place to put that fetch in, than the support file, but it was the best thing I found, when I searched for a suitable place. The support file runs before the tests, the fethc should be at the top of the file depending on what other things you have in there.)

Last sidenote:
Usually a Http Request also returns a Http Response. We don’t actually care about the result. We don’t actually even care if it resolves at all. It only matters for the request to be made. Hence the comment “fire and forgot”. Which is a comment you use to describe “work” you sheduled (like calling an async method) but you didnt wait (await) for the work to complete because you dont care when it finishes.

@RobinMeow solution worked like a charm. Thanks!

go to your support file cypress/support/e2e.ts and add this one line of code: fetch(Cypress.env('baseUrl'), { method: 'GET' }) // fire and forget

This resolved the issue for me, quick and easy without having to change the host.

This issue is painful to me, see my test results below.

Test machines:

  • Linux (RHEL 8): localhost resolves to ::1 first.
  • MacOS (Monterey): localhost resolves to 127.0.0.1 first.
  • Windows 10

Test project:

  • Node 18.18.2
  • Vite 4.4.5 (cannot listen both on ::1 and 127.0.0.1, unless it listens on all interfaces)
  • Cypress 13.4.0
  • Chrome browser

Test cases:

  • Case 1: Vite listens on localhost, Cypress baseUrl set to localhost
    • => Linux: baseUrl check passes, test fails (can’t connect to localhost)
    • => Macos: baseUrl check passes, test fails (can’t connect to localhost)
    • => Windows: both pass
  • Case 2: Vite listens on 127.0.0.1, Cypress baseUrl set to 127.0.0.1
    • => Linux: both pass
    • => MacOS: baseUrl check passes, test fails.
    • => Windows: both pass
  • Case 3: Vite listens on 127.0.0.1, Cypress baseUrl set to localhost
    • => Linux: baseUrl check fails, test passes
    • => MacOS: baseUrl check fails, test passes.
    • => Windows: both pass
  • Case 4: Vite listens on ::1, Cypress baseUrl set to http://[::1]:5173
  • Case 5: Vite listens on all interfaces, Cypress baseUrl set to localhost
    • => Linux: both pass
    • => MacOS: both pass
    • => Windows: both pass

Conclusion:

  • Apart from case 5, I found no configuration that is platform independent and works.
  • Listening on all interfaces might be a security issue.

For me instructing the bundle server (in my case vite) to explicitly use host ‘127.0.0.1’ fixed the issue.

This was solving the issue also on my dev-env. Using v12.9.0