Home > Blockchain >  Cypress CI: Error: connect ECONNREFUSED 127.0.0.1:3000
Cypress CI: Error: connect ECONNREFUSED 127.0.0.1:3000

Time:08-19

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
> [email protected] 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! [email protected] start: `react-scripts --openssl-legacy-provider start`
npm ERR! Exit status 9
npm ERR! 
npm ERR! Failed at the [email protected] 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

CodePudding user response:

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.

CodePudding user response:

can you provide the port like

 - name:            
  • Related