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

运行npx cypress run时遇Cucumber预处理器JSON报告禁用配置错误

解决Cypress Xray插件与Cucumber预处理器JSON报告配置问题

问题根源

cypress-xray-plugin依赖cypress-cucumber-preprocessor生成的JSON报告同步测试结果到Xray,因此必须强制启用该JSON报告功能。

解决方案步骤

1. 更新cypress.config.js配置

在setupNodeEvents函数中添加JSON报告启用配置,根据你的cypress-cucumber-preprocessor版本选择对应配置方式:

适用于v10+版本的cypress-cucumber-preprocessor
const { defineConfig } = require("cypress");
const createBundler = require("@bahmutov/cypress-esbuild-preprocessor");
const { addCucumberPreprocessorPlugin } = require("@badeball/cypress-cucumber-preprocessor");
const createEsbuildPlugin = require("@badeball/cypress-cucumber-preprocessor/esbuild");
const { configureXrayPlugin } = require("cypress-xray-plugin");

module.exports = defineConfig({
  e2e: {
    specPattern: "**/*.feature",
    async setupNodeEvents(on, config) {
      // 初始化Cucumber预处理器
      await addCucumberPreprocessorPlugin(on, config);
      on(
        "file:preprocessor",
        createBundler({
          plugins: [createEsbuildPlugin(config)],
        })
      );
      
      // 启用JSON报告(关键配置)
      config.env["cypress-cucumber-preprocessor"] = config.env["cypress-cucumber-preprocessor"] || {};
      config.env["cypress-cucumber-preprocessor"].json = {
        enabled: true,
        output: "cypress/reports/cucumber-json/cucumber-report.json" // 可自定义输出路径
      };

      // 配置Xray插件
      await configureXrayPlugin(on, config);
      return config;
    },
  },
});
适用于v10以下版本的cypress-cucumber-preprocessor
const { defineConfig } = require("cypress");
const { configureXrayPlugin } = require("cypress-xray-plugin");

module.exports = defineConfig({
  e2e: {
    specPattern: "**/*.feature",
    cucumberOptions: {
      // 启用JSON报告(关键配置)
      json: {
        enabled: true,
        output: "cypress/reports/cucumber-json/cucumber-report.json"
      },
      // 其他Cucumber配置,如步骤定义文件路径
      stepDefinitions: "cypress/e2e/**/*.js"
    },
    async setupNodeEvents(on, config) {
      await configureXrayPlugin(on, config);
      return config;
    },
  },
});

2. 确保输出目录存在

手动创建JSON报告的输出目录(比如上述配置中的cypress/reports/cucumber-json),避免因目录不存在导致报告生成失败。

3. 验证依赖安装

确保已安装所有必要依赖:

npm install @badeball/cypress-cucumber-preprocessor @bahmutov/cypress-esbuild-preprocessor esbuild --save-dev

4. 重新执行测试

运行npx cypress run,验证配置生效,错误不再出现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 10:53:10