Search code examples
node.jscypressgitlab-cicontinuous-deployment

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

Solution

  • 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