Cypress 12.2.0 Docker镜像运行测试报错求助
升级Cypress至12.2.0后Docker环境运行测试报错排查
将Cypress从8版本升级到12.2.0后,本地运行测试套件正常,但使用对应Docker镜像12.2.0运行时出现配置文件崩溃报错,以下是相关信息及排查方案:
报错信息
Your configFile threw an error from: cypress.config.js We stopped running your tests because your config file crashed. Error: ENOENT: no such file or directory, lstat '/support' at Object.lstatSync (fs.js:1007:3) at Object.<anonymous> (/root/.cache/Cypress/12.2.0/Cypress/resources/app/node_modules/@packages/server/node_modules/graceful-fs/polyfills.js:312:16) at Object.lstatSync (/root/.cache/Cypress/12.2.0/Cypress/resources/app/node_modules/@packages/server/node_modules/graceful-fs/polyfills.js:312:16) at Host._follow (/node_modules/tsify/lib/Host.js:278:19) at Host.writeFile (/node_modules/tsify/lib/Host.js:127:29).............
Dockerfile配置
# FROM cypress/base:16.13.0 # FROM cypress/base:12 FROM cypress/base:12.2.0 COPY ./cypress ./cypress COPY ./docker-entrypoint.sh ./docker-entrypoint.sh COPY ./cypress.config.js ./cypress.config.js COPY ./package.json ./package.json COPY ./package-lock.json ./package-lock.json COPY ./docker-entrypoint.sh ./docker-entrypoint.sh COPY ./tsconfig.json ./tsconfig.json RUN chmod +x docker-entrypoint.sh RUN apt-get update && \ export DEBIAN_FRONTEND=noninteractive && \ apt-get -y install \ libgtk2.0-0 \ libgtk-3-0 \ libgbm-dev \ libnotify-dev \ libgconf-2-4 \ libnss3 \ libxss1 \ libasound2 \ libxtst6 xauth xvfb RUN npm install
cypress.config.js配置
const { defineConfig } = require( 'cypress'); const cucumber = require('cypress-cucumber-preprocessor').default const browserify = require('@cypress/browserify-preprocessor'); export default defineConfig({ reporter: 'cypress-mochawesome-reporter', defaultCommandTimeout: 5000, numTestsKeptInMemory: 0, viewportWidth: 1360, viewportHeight: 768, retries: 1, env: { username: 'sxxxxxxxxxx', password: 'xxxxxxxx', }, "video": false, e2e: { chromeWebSecurity: false, setupNodeEvents(on, config) { const options = browserify.defaultOptions; options.browserifyOptions.plugin.unshift(['tsify']); on("file:preprocessor", cucumber(options)); }, defaultCommandTimeout: 10000, specPattern: 'cypress/integration/**/*.feature', supportFile: 'cypress/support/e2e.js' }, })
本地环境
本地Node版本为v16.3.0
Docker运行命令
docker build -t btest:cypre . # 创建镜像 docker run -it --rm --name bContainer -v $PWD/cypress:/cypress btest:cypre # 创建并启动容器 docker exec bContainer npm run bokieLoc -- video=false # 运行测试
package.json配置
{ "scripts": { "bokieLoc": "npx cypress-tags run --env TAGS=\"@home\" --config ", }, "author": "", "license": "ISC", "dependencies": { "ng": "^0.0.0" }, "devDependencies": { "@cypress/browserify-preprocessor": "^3.0.1", "@cypress/webpack-preprocessor": "^5.9.1", "add": "^2.0.6", "cucumber-html-reporter": "^5.5.0", "cypress": "^12.2.0", "cypress-cucumber-attach-screenshots-to-failed-steps": "^1.0.0", "cypress-cucumber-preprocessor": "^4.2.0", "cypress-mochawesome-reporter": "^2.2.0", "cypress-xpath": "^1.6.2", "multiple-cucumber-html-reporter": "^1.18.0", "tsify": "^5.0.4", "typescript": "^4.5.5", "webpack": "^5.61.0", "yarn": "^1.22.10" }, "cypress-cucumber-preprocessor": { "nonGlobalStepDefinitions": true, "cucumberJson": { "generate": true, "outputFolder": "cypress/cucumber_report", "filePrefix": "", "fileSuffix": ".cucumber" } } }
排查方案
1. 修复路径映射与工作目录问题
- 容器启动时的
-v $PWD/cypress:/cypress挂载会覆盖容器内的/cypress目录,导致项目文件分散在根目录和挂载目录,相对路径解析混乱。移除该挂载参数,因为Dockerfile已经通过COPY将本地cypress目录复制到容器内。 - 在Dockerfile中添加工作目录配置,确保所有项目文件在统一路径下:
WORKDIR /app # 后续COPY命令目标路径改为/app下 COPY ./cypress ./cypress COPY ./docker-entrypoint.sh ./docker-entrypoint.sh COPY ./cypress.config.js ./cypress.config.js COPY ./package.json ./package.json COPY ./package-lock.json ./package-lock.json COPY ./tsconfig.json ./tsconfig.json
2. 统一Node版本
Cypress 12.2.0要求Node版本≥14.0.0,当前Docker使用的cypress/base:12.2.0对应Node 12版本,存在兼容性问题。将Docker镜像改为与本地一致的版本:
FROM cypress/base:16.13.0
3. 修复package.json脚本错误
bokieLoc脚本中的--config后未指定配置项,属于无效语法。修改脚本:
"bokieLoc": "npx cypress-tags run --env TAGS=\"@home\" --video false"
若需指定其他配置,补充完整参数,例如--config video=false,viewportWidth=1360。
4. 升级兼容的依赖包
cypress-cucumber-preprocessor@4.2.0版本过旧,与Cypress 12.x不兼容。升级到适配Cypress 12的版本(如v12+),执行:
npm install cypress-cucumber-preprocessor@latest
同时检查@cypress/browserify-preprocessor版本,确保与Cypress 12兼容。
内容的提问来源于stack exchange,提问作者Rama
相关产品推荐
相关产品推荐

