升级Cypress至10.0.0后仍遇cypress-grep版本兼容错误
解决@cypress/grep与Cypress 10.0.0的版本兼容报错问题
排查及解决步骤
1. 升级@cypress/grep到兼容版本
报错提示明确要求cypress-grep 3.0.0+搭配Cypress 10.0.0+,先检查当前安装的插件版本:
npm list @cypress/grep
如果版本低于3.0.0,执行升级命令:
npm install @cypress/grep@latest --save-dev
2. 确认项目本地Cypress版本
全局Cypress版本可能和项目本地版本冲突,用以下命令查看项目实际使用的Cypress版本:
npx cypress version
确保输出显示版本为10.0.0或更高。如果测试脚本里的test:dev2命令调用的是全局Cypress,修改package.json的scripts,改用本地版本:
{ "scripts": { "test:dev2": "npx cypress run --env grep=\"Regression\"" } }
3. 正确配置Cypress 10+的插件加载
Cypress 10+废弃了旧的plugins/index.js配置文件,需在cypress.config.js中注册grep插件:
const { defineConfig } = require('cypress') const grep = require('@cypress/grep/src/plugin') module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { // 注册grep插件 grep(on, config) // 其他项目插件配置... return config }, }, })
同时在cypress/support/e2e.js中导入grep的命令支持:
require('@cypress/grep')()
4. 清理缓存并重新安装依赖
缓存可能导致版本识别异常,执行以下步骤:
- 删除依赖目录和锁文件:
# macOS/Linux rm -rf node_modules package-lock.json # Windows rmdir /s /q node_modules && del package-lock.json - 重新安装依赖:
npm install - 清理Cypress缓存:
npx cypress cache clear
5. 优化测试用例标签格式(可选)
当前测试用例的tags参数写法可以优化,将合并的字符串拆分为数组元素,方便后续更精准的标签筛选:
it('Validate Default signature is being populated Reply to all email task (Regression,Sanity)',{tags: ['@Regression', '@Sanity']}, () => { // 测试代码... })
之后可以用--env grepTags=@Regression单独筛选Regression标签的用例。
内容的提问来源于stack exchange,提问作者Neetu Gupta
相关产品推荐
相关产品推荐

