如何在Angular NX工作区中运行带标签的NX e2e测试
解决方案
问题原因
你直接向nx e2e命令传入的--tags参数不会被NX默认透传给底层的Cucumber预处理器,因此无法触发标签筛选逻辑,导致所有测试都会运行。同时你原命令中--baseUrl=后面的多余空格会导致参数识别错误,需要先删掉。
单项目按标签筛选测试
使用--env参数将tags透传给Cypress,正确命令如下:
nx e2e <projectName> --baseUrl=adc.com --env tags="@Regression"
受影响应用按标签筛选测试
nx affected:e2e同样通过--env透传标签参数:
nx affected:e2e --baseUrl=adc.com --env tags="@Regression"
可选:配置Cypress支持环境变量读取标签
如果上述命令不生效,你可以修改项目下的cypress.config.ts/cypress.config.js,增加标签读取逻辑:
import { defineConfig } from 'cypress' import createBundler from '@bahmutov/cypress-esbuild-preprocessor' import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor' import { createEsbuildPlugin } from '@badeball/cypress-cucumber-preprocessor/esbuild' export default defineConfig({ e2e: { async setupNodeEvents(on, config) { await addCucumberPreprocessorPlugin(on, config) on( 'file:preprocessor', createBundler({ plugins: [createEsbuildPlugin(config)], }) ) // 从环境变量读取标签配置 config.env.tags = process.env.TAGS ?? config.env.tags return config }, }, cucumber: { tags: process.env.TAGS, }, })
配置完成后也可以通过环境变量传入标签:
TAGS="@Regression" nx e2e <projectName> --baseUrl=adc.com
多标签筛选规则
- 同时匹配多个标签:
tags="@Regression and @Smoke" - 匹配任意多个标签:
tags="@Regression or @Smoke" - 排除指定标签:
tags="@Regression and not @Ignore"
注:上述配置适配当前主流维护的
@badeball/cypress-cucumber-preprocessor v12+版本,如果你使用的是已停止维护的旧版cypress-cucumber-preprocessor,需将环境变量名替换为CYPRESS_TAGS。
内容的提问来源于stack exchange,提问作者saikumark
相关产品推荐
相关产品推荐

