cypress-io / github-action

GitHub Action for running Cypress end-to-end & component tests
https://on.cypress.io/guides/continuous-integration/github-actions
MIT License
1.37k stars 345 forks source link

localhost not found - Error: ESOCKETTIMEDOUT #1006

Closed Marcel-Malus closed 1 year ago

Marcel-Malus commented 1 year ago

I'm unable to run the standard GitHub action suggested in your README. After trying all kinds of variations and fix suggestions I run out of ideas and would appreciate some guidance :) According to the GitHub Action logs the WebApp starts normally, but somehow the cypress testing framework doesn't find it when trying to access it. Also everything works fine locally, using the same config / tests.

Cypress config:

import { defineConfig } from "cypress";

export default defineConfig({
  e2e: {
    baseUrl: 'http://localhost:4200',
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
});

Start script: "start": "ng version && ng serve --disable-host-check",

GitHub Action (just the jobs part):

jobs:
  cypress-run:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: '16.15'
      - name: Cypress run
        uses: cypress-io/github-action@v5
        with:
          start: npm run start
          browser: chrome
          wait-on: 'http://localhost:4200'

What I tried to so far:

Any help appreciated, thanks!

MikeMcC399 commented 1 year ago

@Marcel-Malus

Are you able to share your repo or is this a private one?

Which versions of

are you using?

Marcel-Malus commented 1 year ago

Unfortunately it's a private company repo, which I can't share.

Versions of:

MikeMcC399 commented 1 year ago

@Marcel-Malus

Thank you for the version specs. That is very helpful.

Can you post the relevant part of your log file showing the error?

Marcel-Malus commented 1 year ago

This is what I get when not having the wait-on:

1) home screen
       displays:
     CypressError: `cy.visit()` failed trying to load:

http://localhost:4200/en/

We attempted to make an http request to this URL but the request failed without a response.

We received this error at the network level:

  > Error: ESOCKETTIMEDOUT

Common situations why this would fail:
  - you don't have internet access
  - you forgot to run / boot your web server
  - your web server isn't accessible
  - you have weird network configuration settings on your computer

This error occurred while creating the session. Because the session setup failed, we failed the test.
      at <unknown> (http://localhost:4200/__cypress/runner/cypress_runner.js:130312:74)
      at visitFailedByErr (http://localhost:4200/__cypress/runner/cypress_runner.js:129704:12)
      at <unknown> (http://localhost:4200/__cypress/runner/cypress_runner.js:130311:11)
      at tryCatcher (http://localhost:4200/__cypress/runner/cypress_runner.js:[180](https://github.com/tadodotcom/webapp2018/actions/runs/5972533235/job/16203224191#step:4:181)7:23)
      at Promise._settlePromiseFromHandler (http://localhost:4200/__cypress/runner/cypress_runner.js:1519:31)
      at Promise._settlePromise (http://localhost:4200/__cypress/runner/cypress_runner.js:1576:18)
      at Promise._settlePromise0 (http://localhost:4200/__cypress/runner/cypress_runner.js:1621:10)
      at Promise._settlePromises (http://localhost:4200/__cypress/runner/cypress_runner.js:1697:18)
      at _drainQueueStep (http://localhost:4[200](https://github.com/tadodotcom/webapp2018/actions/runs/5972533235/job/16203224191#step:4:201)/__cypress/runner/cypress_runner.js:2407:12)
      at _drainQueue (http://localhost:4200/__cypress/runner/cypress_runner.js:2400:9)
      at Async._drainQueues (http://localhost:4200/__cypress/runner/cypress_runner.js:2416:5)
      at Async.drainQueues (http://localhost:4200/__cypress/runner/cypress_runner.js:2286:14)
  From Your Spec Code:
      at Object.eval [as setup] (webpack://webapp[201](https://github.com/tadodotcom/webapp2018/actions/runs/5972533235/job/16203224191#step:4:202)8/./cypress/support/commands.ts:18:9)

  From Node.js Internals:
    Error: ESOCKETTIMEDOUT
        at ClientRequest.<anonymous> (<embedded>:[208](https://github.com/tadodotcom/webapp2018/actions/runs/5972533235/job/16203224191#step:4:209)6:99696)
        at Object.onceWrapper (node:events:641:28)
        at ClientRequest.emit (node:events:527:28)
        at Socket.emitRequestTimeout (node:_http_client:771:9)
        at Object.onceWrapper (node:events:641:28)
        at Socket.emit (node:events:539:35)
        at Socket._onTimeout (node:net:516:8)
        at listOnTimeout (node:internal/timers:559:17)
        at process.processTimers (node:internal/timers:502:7)

This is what I get when using wait-on (in this case I also tried the IP for localhost, but the same error happens with using localhost:

http://127.0.0.1:4200/ timed out on retry 12 of 3, elapsed 100108ms, limit 90000ms
Error: Timeout awaiting 'request' for 30000ms
MikeMcC399 commented 1 year ago

@Marcel-Malus

I checked with

and I was able to run a Cypress end-to-end test successfully.

For Node.js 16 there is normally no need to substitute a numerical loopback address instead of using localhost.

Error: ESOCKETTIMEDOUT may be due to the server just not being ready if you do not use wait-on.

I suggest you keep wait-on configured and test with a higher timeout value e.g. wait-on-timeout: 120 for 2 minutes. (see https://github.com/cypress-io/github-action#wait-on).

If you need more information in the logs add

  env:
    DEBUG: '@cypress/github-action'

(see https://github.com/cypress-io/github-action#debugging).

Marcel-Malus commented 1 year ago

So tried that and still no luck. One of the last debug logs (with the extended cypress debug logs enabled) is:

http://localhost:4200 timed out on retry 14 of 5, elapsed 161145ms, limit 150000ms
2023-08-25T09:55:20.289Z @cypress/github-action Timeout awaiting 'request' for 30000ms
2023-08-25T09:55:20.289Z @cypress/github-action RequestError: Timeout awaiting 'request' for 30000ms
Error: Timeout awaiting 'request' for 30000ms
    at ClientRequest.<anonymous> (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:51570:65)
    at Object.onceWrapper (node:events:628:26)
    at ClientRequest.emit (node:events:525:35)
    at ClientRequest.origin.emit (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:44643:20)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
    at Timeout.timeoutHandler [as _onTimeout] (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:52352:25)
    at listOnTimeout (node:internal/timers:561:11)
    at processTimers (node:internal/timers:502:7)

I was wondering if the problem could be somewhere in the GitHub action settings? Just checked them and under Actions:General everything is allowed / enabled. But under Actions:Runners Public IP: Disabled is set. I would assume accessing localhost does not fall under 'Public IP' but maybe I'm mistaken? image

MikeMcC399 commented 1 year ago

@Marcel-Malus

If the server were not reachable at all, then you would get something like the following:

2023-08-25T11:08:48.496Z @cypress/github-action connect ECONNREFUSED 127.0.0.1:4200
2023-08-25T11:08:48.496Z @cypress/github-action RequestError: connect ECONNREFUSED 127.0.0.1:4200
    at ClientRequest.<anonymous> (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:51570:111)
    at Object.onceWrapper (node:events:628:26)
    at ClientRequest.emit (node:events:525:35)
    at ClientRequest.origin.emit (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:44643:20)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
Error: connect ECONNREFUSED 127.0.0.1:4200

Perhaps you could try start: npx ng serve --disable-host-check instead of using the npm start command?

If that doesn't work, then please post your full debug log.

PS: I tried using "start": "ng version && ng serve --disable-host-check", and it didn't seem to cause an issue.

Marcel-Malus commented 1 year ago

Tried the suggested start script and also no luck 😕 Here is the complete log of the Cypress run job:

Run cypress-io/github-action@v5
2023-08-28T06:58:25.787Z @cypress/github-action working directory /home/runner/work/webapp2018/webapp2018
2023-08-28T06:58:25.789Z @cypress/github-action using custom Cypress cache folder "/home/runner/.cache/Cypress"
2023-08-28T06:58:25.789Z @cypress/github-action trying to restore cached npm modules
2023-08-28T06:58:25.795Z @cypress/github-action Hash from file /home/runner/work/webapp2018/webapp2018/package-lock.json is aa482b2e945a06431ffa4b303a014f8d226deef090d685300dffffed0889177b9f27b951ca72e534ea3433f841289740ebc4c90[73](https://github.com/tadodotcom/webapp2018/actions/runs/5996646078/job/16261549715#step:4:74)99a91e7aee9c13e82b0db2f
2023-08-28T06:58:25.798Z @cypress/github-action trying to restore cached Cypress binary
2023-08-28T06:58:25.802Z @cypress/github-action Hash from file /home/runner/work/webapp2018/webapp2018/package-lock.json is aa482b2e945a06431ffa4b303a014f8d226deef090d685300dffffed0889177b9f27b951ca72e534ea3433f841289[74](https://github.com/tadodotcom/webapp2018/actions/runs/5996646078/job/16261549715#step:4:75)0ebc4c907399a91e7aee9c13e82b0db2f
Received 16777216 of 170065094 (9.9%), 16.0 MBs/sec
Received 16777216 of 206151632 (8.1%), 16.0 MBs/sec
Received 67108864 of 170065094 (39.5%), 32.0 MBs/sec
Received 88080384 of 206151632 (42.7%), 42.0 MBs/sec
Received 109051904 of 170065094 (64.1%), 34.7 MBs/sec
Received 130023424 of 206151632 (63.1%), 41.3 MBs/sec
Received 134217728 of 170065094 (78.9%), 32.0 MBs/sec
Received 155189248 of 206151632 ([75](https://github.com/tadodotcom/webapp2018/actions/runs/5996646078/job/16261549715#step:4:76).3%), 37.0 MBs/sec
Received 167772160 of 170065094 (98.7%), 32.0 MBs/sec
Received 193568720 of 206151632 (93.9%), 36.9 MBs/sec
Received 170065094 of 170065094 (100.0%), 31.5 MBs/sec
Cache Size: ~162 MB (170065094 B)
/usr/bin/tar -xf /home/runner/work/_temp/a5a42924-c861-45d4-885c-e843b8747012/cache.tzst -P -C /home/runner/work/webapp2018/webapp2018 --use-compress-program unzstd
Received 206151632 of 206151632 (100.0%), 32.5 MBs/sec
Cache Size: ~197 MB (206151632 B)
/usr/bin/tar -xf /home/runner/work/_temp/a89615e3-bb9e-4b0d-a4e3-ce2ed69f3ee3/cache.tzst -P -C /home/runner/work/webapp2018/webapp2018 --use-compress-program unzstd
Cache restored successfully
Cache restored successfully
2023-08-28T06:58:35.425Z @cypress/github-action npm cache hit npm-linux-x64-aa482b2e945a06431ffa4b303a014f8d226deef090d685300dffffed0889177b9f27b951ca72e534ea3433f841289740ebc4c907399a91e7aee9c13e82b0db2f
2023-08-28T06:58:35.425Z @cypress/github-action cypress cache hit cypress-linux-x64-aa482b2e945a06431ffa4b303a014f8d226deef090d685300dffffed0889177b9f27b951ca72e534ea3433f841289740ebc4c907399a91e7aee9c13e82b0db2f
2023-08-28T06:58:35.428Z @cypress/github-action installing npm dependencies
2023-08-28T06:58:35.432Z @cypress/github-action npm at "/opt/hostedtoolcache/node/16.20.1/x64/bin/npm"
/opt/hostedtoolcache/node/16.20.1/x64/bin/npm ci
npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated @npmcli/move-file@2.0.1: This functionality has been moved to @npmcli/fs
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated karma-phantomjs-launcher@1.0.4: PhantomJS development have stopped, use puppeteer or similar
npm WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated uglify-es@3.3.9: support for ECMAScript is superseded by `uglify-js` as of v3.13.0
npm WARN deprecated protractor@7.0.0: We have news to share - Protractor is deprecated and will reach end-of-life by Summer 2023. To learn more and find out about other options please refer to this post on the Angular blog. Thank you for using and contributing to Protractor. https://goo.gle/state-of-e2e-in-angular

added 2263 packages, and audited 2264 packages in 34s

178 packages are looking for funding
  run `npm fund` for details

19 vulnerabilities (16 moderate, 3 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.
2023-08-28T06:59:09.521Z @cypress/github-action install has finished
2023-08-28T06:59:09.521Z @cypress/github-action Cypress versions in the cache folder /home/runner/.cache/Cypress
/opt/hostedtoolcache/node/16.20.1/x64/bin/npx cypress cache list
┌─────────┬───────────────────┐
│ version │ last used         │
├─────────┼───────────────────┤
│ 12.17.4 │ a few seconds ago │
└─────────┴───────────────────┘
2023-08-28T06:59:10.182Z @cypress/github-action no need to verify Cypress binary or save caches
2023-08-28T06:59:10.182Z @cypress/github-action Separated 1 start commands npm run start-cypress
start server command "npm run start-cypress"
current working directory "/home/runner/work/webapp2018/webapp2018"
2023-08-28T06:59:10.183Z @cypress/github-action parsed command: npm run start-cypress
waiting on "http://localhost:4200" with timeout of 120 seconds
2023-08-28T06:59:10.184Z @cypress/github-action Waiting for urls http://localhost:4200
2023-08-28T06:59:10.184Z @cypress/github-action Waiting for url http://localhost:4200
2023-08-28T06:59:10.184Z @cypress/github-action total ping timeout 150000
2023-08-28T06:59:10.184Z @cypress/github-action individual ping timeout 30000ms
2023-08-28T06:59:10.184Z @cypress/github-action retries limit 5
2023-08-28T06:59:10.200Z @cypress/github-action got error {"name":"RequestError","code":"ECONNREFUSED","timings":{"start":1693205950189,"socket":1693205950190,"lookup":1693205950191,"error":1693205950192,"phases":{"wait":1,"dns":1,"total":3}}}
2023-08-28T06:59:10.200Z @cypress/github-action 16ms undefined undefined ECONNREFUSED attempt 1
2023-08-28T06:59:10.202Z @cypress/github-action found command "/opt/hostedtoolcache/node/16.20.1/x64/bin/npm"
2023-08-28T06:59:10.202Z @cypress/github-action with arguments run start-cypress
2023-08-28T06:59:10.202Z @cypress/github-action running "/opt/hostedtoolcache/node/16.20.1/x64/bin/npm" run start-cypress in /home/runner/work/webapp2018/webapp2018
2023-08-28T06:59:10.202Z @cypress/github-action waiting for the command to finish? false
/opt/hostedtoolcache/node/16.20.1/x64/bin/npm run start-cypress

> webapp2018@0.0.0 start-cypress
> npx ng serve --disable-host-check

2023-08-28T06:59:11.204Z @cypress/github-action got error {"name":"RequestError","code":"ECONNREFUSED","timings":{"start":1693205951203,"socket":1693205951203,"lookup":1693205951204,"error":1693205951204,"phases":{"wait":0,"dns":1,"total":1}}}
2023-08-28T06:59:11.204Z @cypress/github-action 1020ms undefined undefined ECONNREFUSED attempt 2
2023-08-28T06:59:12.209Z @cypress/github-action got error {"name":"RequestError","code":"ECONNREFUSED","timings":{"start":1693205952207,"socket":1693205952207,"lookup":1693205952208,"error":1693205952208,"phases":{"wait":0,"dns":1,"total":1}}}
2023-08-28T06:59:12.209Z @cypress/github-action 2025ms undefined undefined ECONNREFUSED attempt 3
Warning: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.
2023-08-28T06:59:13.212Z @cypress/github-action got error {"name":"RequestError","code":"ECONNREFUSED","timings":{"start":1693205953211,"socket":1693205953211,"lookup":1693205953211,"error":1693205953212,"phases":{"wait":0,"dns":0,"total":1}}}
2023-08-28T06:59:13.212Z @cypress/github-action 3028ms undefined undefined ECONNREFUSED attempt 4
2023-08-28T06:59:14.215Z @cypress/github-action got error {"name":"RequestError","code":"ECONNREFUSED","timings":{"start":1693205954214,"socket":1693205954214,"lookup":1693205954215,"error":1693205954215,"phases":{"wait":0,"dns":1,"total":1}}}
2023-08-28T06:59:14.215Z @cypress/github-action 4031ms undefined undefined ECONNREFUSED attempt 5
- Generating browser application bundles (phase: setup)...
2023-08-28T06:59:45.221Z @cypress/github-action got error {"name":"TimeoutError","code":"ETIMEDOUT","timings":{"start":1693205955217,"socket":1693205955218,"lookup":1693205955218,"connect":1693205955218,"upload":1693205955219,"error":1693205985220,"phases":{"wait":1,"dns":0,"tcp":0,"request":1,"total":30003}},"event":"request"}
2023-08-28T06:59:45.221Z @cypress/github-action 35037ms undefined undefined ETIMEDOUT attempt 6
2023-08-28T07:00:45.2[76](https://github.com/tadodotcom/webapp2018/actions/runs/5996646078/job/16261549715#step:4:77)Z @cypress/github-action got error {"name":"TimeoutError","code":"ETIMEDOUT","timings":{"start":1693206015249,"socket":1693206015250,"lookup":1693206015250,"connect":1693206015250,"upload":1693206015250,"error":1693206045276,"phases":{"wait":1,"dns":0,"tcp":0,"request":0,"total":30027}},"event":"request"}
2023-08-28T07:00:45.276Z @cypress/github-action 95092ms undefined undefined ETIMEDOUT attempt 7
2023-08-28T07:01:45.311Z @cypress/github-action got error {"name":"TimeoutError","code":"ETIMEDOUT","timings":{"start":1693206075308,"socket":1693206075308,"lookup":1693206075308,"connect":1693206075308,"upload":1693206075308,"error":1693206105311,"phases":{"wait":0,"dns":0,"tcp":0,"request":0,"total":30003}},"event":"request"}
2023-08-28T07:01:45.311Z @cypress/github-action 155127ms undefined undefined ETIMEDOUT attempt 8
Error: Timeout awaiting 'request' for 30000ms
http://localhost:4200 timed out on retry 8 of 5, elapsed 155127ms, limit 150000ms
2023-08-28T07:01:45.312Z @cypress/github-action Timeout awaiting 'request' for 30000ms
2023-08-28T07:01:45.312Z @cypress/github-action RequestError: Timeout awaiting 'request' for 30000ms
    at ClientRequest.<anonymous> (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:51570:65)
    at Object.onceWrapper (node:events:628:26)
    at ClientRequest.emit (node:events:525:35)
    at ClientRequest.origin.emit (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:44643:20)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:[83](https://github.com/tadodotcom/webapp2018/actions/runs/5996646078/job/16261549715#step:4:84):21)
    at Timeout.timeoutHandler [as _onTimeout] (/home/runner/work/_actions/cypress-io/github-action/v5/dist/index.js:52352:25)
    at listOnTimeout (node:internal/timers:561:11)
    at processTimers (node:internal/timers:502:7)
MikeMcC399 commented 1 year ago

@Marcel-Malus

I can't see any line in your log file which says:

Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/

so it looks like your server is not starting.

To take github-action out of the equation you could replace the step which calls gitthub-action using something like the following:

      - name: Install npm dependencies
        run: npm ci

      - name: Start server
        run: npm start &

      - name: Sleep for 60 seconds
        run: sleep 60s
        shell: bash

      - name: Check website availability
        run: curl -I http://localhost:4200

      - name: Test the website
        run: npx cypress run

You might need to tweak the length of time that the workflow sleeps if you have a complex application which takes a long time to start.

Marcel-Malus commented 1 year ago

It finally worked 🥳 thanks so much! The curl script was the most helpful part, because it was pinging the WebApp until it was finally up and blocking further steps. With that I also learned, that I get a 404 on that address and actually need to ping http://localhost:4200/en instead.

With both learnings I could build it back to be working with the cypress GH Action again by changing 2 things:

Thanks a lot for your patience and support! Really much appreciated 😊

MikeMcC399 commented 1 year ago

@Marcel-Malus

It's good to hear that you have resolved your issue!

Change the wait-on to http://localhost:4200/en. I would kindly suggest to add that maybe to the README as I guess localisations are quite common?

I'm not sure that would be a good general recommendation. http://localhost:4200 should probably be responding and forwarding to your default language. Normally I would expect that baseUrl would function stand-alone, and that the URL used in the wait-on parameter is capable of responding correctly.

If you think that something should be added to the README, then you could submit a PR with your suggestions.

Would you like to now close this issue, since it is resolved?

Marcel-Malus commented 1 year ago

Yes for our production setup we have a redirect, but not for the local development/test setup as we run also just one locale (not all of them). It might be a specific Angular setup and I understand, that this testing framework is not specific to Angular, so I totally get the doubt to not add it to the README. Still, maybe just sth. to consider: The curl script recognised the server being up by receiving a 404. Maybe that could be enough for the wait-on to receive a 404? The actual tests can run under more explicit sub-urls (in my case also including the locale) and still work. Just a suggestion :)

I will close this now as my original issue is resolved now. Thanks again 😊