You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在同一仓库运行*.feature与*.cy.ts测试?Cypress配置咨询

同时运行Cucumber .feature和Cypress .cy.ts测试文件的正确配置

问题根源在于Cucumber预处理器会接管所有测试文件的处理流程,导致普通.cy.ts文件无法被正常解析;同时原specPattern的管道符写法存在匹配逻辑优先级问题。以下是修正后的配置方案:

1. 修正specPattern配置

将specPattern改为数组形式,避免匹配冲突,同时更清晰定义测试文件范围:

specPattern: ['cypress/e2e/**/*.feature', 'cypress/e2e/**/*.cy.ts'],

2. 按文件类型区分预处理器

在setupNodeEvents中添加逻辑,仅对.feature文件应用Cucumber预处理器,.cy.ts文件使用常规Cypress预处理器:

修改后的cypress.config.ts完整e2e配置片段:

import { defineConfig } from 'cypress';
import { createBundler } from '@bahmutov/cypress-esbuild-preprocessor';
import { createEsbuildPlugin } from '@badeball/cypress-cucumber-preprocessor/esbuild';
import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';

export default defineConfig({
  e2e: {
    async setupNodeEvents(on, config) {
      // 为Cucumber测试文件创建专用bundler
      const cucumberBundler = createBundler({
        plugins: [createEsbuildPlugin(config)],
      });

      // 自定义预处理器,根据文件扩展名分配处理逻辑
      on('file:preprocessor', (file) => {
        if (file.filePath.endsWith('.feature')) {
          return cucumberBundler(file);
        }
        // 为普通Cypress测试文件创建常规bundler
        const regularBundler = createBundler({
          plugins: [createEsbuildPlugin(config)],
        });
        return regularBundler(file);
      });

      await addCucumberPreprocessorPlugin(on, config);

      // 配置Cucumber步骤定义文件路径(根据项目实际结构调整)
      config.cucumber = {
        stepDefinitions: 'cypress/e2e/**/*.steps.ts',
      };

      return config;
    },
    specPattern: ['cypress/e2e/**/*.feature', 'cypress/e2e/**/*.cy.ts'],
  },
});

3. 验证效果

重启Cypress后:

  • .feature文件会通过Cucumber预处理器解析,正常执行Gherkin场景
  • .cy.ts文件会使用常规预处理器,按普通Cypress测试逻辑运行

内容的提问来源于stack exchange,提问作者LukaRls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:20:27