对于一个小项目,我正在尝试针对 Nodejs 应用程序运行 Cypress 测试,该应用程序是我从 GitHub 中获取的示例。此外,我想在 GitLab CI/CD 中执行测试。我的 yml 文件看起来像这样:
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
这里是我作为示例的应用程序的参考,这里是Cypress测试。
我得到的错误是这样的:
"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`
更新 运行 sleep 和curl 命令后,结果如下:
$ 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
错误本身已经提供了最可能的原因:
在您的情况下,您似乎没有启动服务器。
查看您的 CI 脚本,您正在启动服务器,然后立即尝试运行 cypress 测试。服务器可能没有这么快启动。试试这个:
- npm run start &
- sleep 5
- npm run cypress
这将迫使
npm run cypress
在开始之前等待 5 秒。
你能提供端口吗
- name: 🌳 Cypress run
uses: cypress-io/github-action@v4
with:
start: npm run xx
wait-on: "http://localhost:8811"
希望能帮到你
发现问题了!
问题是 React 的一个选项,npm 不支持该选项:
--openssl-legacy-provider
删除它,我能够卷曲节点服务器并运行 cypress 测试
在运行 cypress 之前,请确保您的服务器(通常是本地开发服务器)正在运行。