简体   繁体   中英

Cypress CI: Error: connect ECONNREFUSED 127.0.0.1:3000

for a small project i'm trying to run Cypress tests against a Nodejs app that i took from an example in GitHub. Additionally, i want to execute the tests in GitLab CI/CD. My yml file looks something like this:

stages:
 - build
 - publish # (consumer only)
 - can-i-deploy
 - deploy
 - tag

pact-test:
  image: $CI_REGISTRY_IMAGE
  stage: build
  script:
    - npm ci
    - npm run start &
    - npm run cypress
  artifacts:
    paths:
      - pacts

Here is the reference to the application i took as example, and here are the Cypress tests .

The error i get is this:

 "before each" hook for "displays product item":
     CypressError: `cy.visit()` failed trying to load:
http://localhost:3000/products/09
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: connect ECONNREFUSED 127.0.0.1:3000
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
Because this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `product page`

UPDATE After running a sleep and a curl command this is the result:

$ npm run start &
$ sleep 5
> poc-consumer@0.1.0 start /builds/poc-consumer
> react-scripts --openssl-legacy-provider start
node: bad option: --openssl-legacy-provider
npm ERR! code ELIFECYCLE
npm ERR! errno 9
npm ERR! poc-consumer@0.1.0 start: `react-scripts --openssl-legacy-provider start`
npm ERR! Exit status 9
npm ERR! 
npm ERR! Failed at the poc-consumer@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-08-18T13_20_15_778Z-debug.log
$ curl http://localhost:3000/products/09
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 3000: Connection refused

The error itself already provides the most likely reasons:

  • 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

In your case, it appears that you did not start the server.

Looking at your CI script, you are starting the server and then immediately attempt to run tests. Chances are that server did not start this quick. Try this instead:

- npm run start &
- sleep 5
- npm run cypress

This will force npm run cypress to wait 5 seconds before starting.

can you provide the port like

 - name: 🌳 Cypress run
   uses: cypress-io/github-action@v4
   with:
     start: npm run xx
     wait-on: "http://localhost:8811"

hope it can be help

Problem found!

The issue was an option for react, that is not supported from npm: --openssl-legacy-provider removed that, i was able to curl the node server and run the cypress test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM