GitHub Action中Cypress无法连接localhost的json-server问题求助
问题:GitHub Action中Cypress无法连接本地json-server(ECONNREFUSED)
我有一个基于HTML/JS前端、由json-server托管JSON数据库的基础应用,已搭建包含2个测试用例的Cypress端到端测试套件,本地运行完全正常,但在GitHub Action中运行时出现ECONNREFUSED错误,无法连接http://localhost:3000/todos。尽管GitHub Action的日志显示json-server已启动且/todos资源可用,但测试仍报错。
错误日志
Running: standard-criteria.spec.js (1 of 1) Todo List Standard Criteria 1) "before each" hook for "Loads and renders the Todo List from the server" 0 passing (1s) 1 failing 1) Todo List Standard Criteria "before each" hook for "Loads and renders the Todo List from the server": CypressError: `cy.request()` failed trying to load: http://localhost:3000/todos 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 ----------------------------------------------------------- The request we sent was: Method: GET URL: http://localhost:3000/todos -----------------------------------------------------------
服务启动日志
Run cypress-io/github-action@v5.0.9 Received 6752398 of 6752398 (100.0%), 9.8 MBs/sec Cache Size: ~6 MB (6752398 B) /usr/bin/tar -xf /home/runner/work/_temp/3f18ede8-26a0-4695-a13b-b89f0d335c5b/cache.tgz -P -C /home/runner/work/js-dom-todos/js-dom-todos -z Cache restored successfully Received 134217728 of 169363420 (79.2%), 128.0 MBs/sec Received 169363420 of 169363420 (100.0%), 126.8 MBs/sec Cache Size: ~162 MB (169363420 B) /usr/bin/tar -xf /home/runner/work/_temp/821a36e6-5b1e-4be7-a3fa-063c54775cb3/cache.tgz -P -C /home/runner/work/js-dom-todos/js-dom-todos -z Cache restored successfully /usr/local/bin/npm ci added 324 packages, and audited 325 packages in 11s 46 packages are looking for funding run `npm fund` for details 9 vulnerabilities (5 moderate, 4 high) To address all issues, run: npm audit fix Run `npm audit` for details. /usr/local/bin/npx cypress cache list ┌─────────┬───────────────────┐ │ version │ last used │ ├─────────┼───────────────────┤ │ 12.6.0 │ a few seconds ago │ └─────────┴───────────────────┘ start server command "npm run dev:test" current working directory "/home/runner/work/js-dom-todos/js-dom-todos" /usr/local/bin/npm run dev:test > dev:test > npx json-server --watch db/test_todos.json [1924:0223/102500.289570:ERROR:gpu_memory_buffer_support_x11.cc(44)] dri3 extension not supported. \{^_^}/ hi! Loading db/test_todos.json Done Resources http://localhost:3000/todos Home http://localhost:3000 Type s + enter at any time to create a snapshot of the database Watching...
相关配置文件
GitHub Action配置文件
name: Standard Criteria on: pull_request: branches: [ main, jules-cypress-test ] jobs: cypress-e2e-tests: runs-on: ubuntu-latest strategy: matrix: node-version: [ 16.19.1 ] steps: - name: Checkout uses: actions/checkout@v3 - name: Cypress run uses: cypress-io/github-action@v5.0.9 # Released 2023-02-13 with: browser: chrome
cypress.config.js
const { defineConfig } = require("cypress"); module.exports = defineConfig({ env: { apiUrl: "http://localhost:3000/todos", }, e2e: { experimentalStudio: true, specPattern: "cypress/tests/**/*.spec.{js,jsx,ts,tsx}", // setupNodeEvents(on, config) { // } } });
测试用例文件
describe('Todo List', () => { beforeEach(() => { cy.visit("index.html"); cy.request(Cypress.env("apiUrl")).as("todos"); }); describe('Standard Criteria', () => { it('Loads and renders the Todo List from the server', () => { cy.get("@todos").should((response) => { expect(response.body).to.have.length(3) }) ...
已尝试的解决方案
- 将Node版本从V18降级到V16;
- 在Action中切换Electron和Chrome浏览器;
- 在Action配置中设置
wait-on和超时时间; - 添加curl步骤测试端点,结果正常;
- 修改json-server启动命令为
npx json-server --watch db/test_todos.json --port 3000 --no-cors。
内容的提问来源于stack exchange,提问作者Jules Nuggy
相关产品推荐
相关产品推荐

