Cypress: Using a subdomain with localhost will throw an error of unreachable on Mac

Created on 22 Mar 2018  路  7Comments  路  Source: cypress-io/cypress

Current behavior:

If you set up a visit call to go to something like mysubdomain.localhost:4200 Cypress will not respond properly (and will not visit) because it uses the hosts file for some subdomain redirection on localhost.

Desired behavior:

I shouldn't have to add 127.0.0.1 for EVERY subdomain I want to test on my localhost, especially if I generate these for tests. Cypress should not have to rely on the hosts file for this.

How to reproduce:

Create a server that runs on a subdomain and your local machine so the address is something like mysubdomain.localhost:4200. Attempt to run a test that goes there. It will fail unless you add

127.0.0.1 mysubdomain.localhost

to your hosts file on Mac

  • Operating System: OSX
  • Cypress Version: 2.10
  • Browser Version: Chrome 64

Most helpful comment

Sure we can look into adding this. @Bkucera is right that I don't believe this works by default - when the DNS queries come back - it will not be your machine, but we can add overrides to automatically do this.

BTW there is an undocumented option called hosts in cypress.json that allows you to do this. It's undocumented because the implementation is kind of hacky but it does work, and we use it in our own internal tests so we test how cypress behaves on other domains, which are mapped back to loopback.

// cypress.json
"hosts": {
  "*.localhost": "127.0.0.1"
}

I haven't tried this but our e2e tests do something similar and you could do a quick search in our repo for various implementations.

Here's one example: https://github.com/cypress-io/cypress/blob/develop/packages/driver/test/cypress.json#L4

All 7 comments

@OrganicCat If my understanding is correct, you can't access a subdomain on localhost without it being added to your hosts file. This is true for any browser, Cypress driven or not. Could you explain your desired behavior in this case for Cypress vs a typical web browser accessing localhost subdomains? -
this could possibly be implemented as a feature

This is not true for a webpack build set up like so:

devServer: {
        historyApiFallback: true,
        allowedHosts: ['.localhost']
    }

This requires no additional setup in my hosts file, which is good because our program requires us to set up a lot of dynamic subdomains. Now if this were deployed in a remote location, yes that would require additional setup. But locally, no.

Additionally, if this is going to be classified as a feature, please at least add warning info if someone is attempting this, or notes or something, because right now cypress doesn't work with no useful information given.

I have the same problem, trying to open http://booking.localhost:8080 results in:

Error: getaddrinfo ENOTFOUND booking.localhost booking.localhost:8080
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26

I can open this page in any browser without problems.

exact same problem here

Sure we can look into adding this. @Bkucera is right that I don't believe this works by default - when the DNS queries come back - it will not be your machine, but we can add overrides to automatically do this.

BTW there is an undocumented option called hosts in cypress.json that allows you to do this. It's undocumented because the implementation is kind of hacky but it does work, and we use it in our own internal tests so we test how cypress behaves on other domains, which are mapped back to loopback.

// cypress.json
"hosts": {
  "*.localhost": "127.0.0.1"
}

I haven't tried this but our e2e tests do something similar and you could do a quick search in our repo for various implementations.

Here's one example: https://github.com/cypress-io/cypress/blob/develop/packages/driver/test/cypress.json#L4

works like a charm, thanks for the quick answer @brian-mann!

Closing as resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t-zander picture t-zander  路  125Comments

chrisbreiding picture chrisbreiding  路  114Comments

cameronc56 picture cameronc56  路  77Comments

verheyenkoen picture verheyenkoen  路  84Comments

amirrustam picture amirrustam  路  66Comments