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

使用Cypress运行Cucumber测试时cypress.config.js报错求助

问题:Cypress运行Cucumber测试时配置文件崩溃报错

运行Cucumber测试时弹出报错:

Your configFile threw an error from: cypress.config.js
We stopped running your tests because your config file crashed.

已尝试升级/降级Node版本、更换不同版本的cypress-cucumber-preprocessor,问题仍未解决。

相关代码片段

index.js

const createEsbuildPlugin =
    require('@badeball/cypress-cucumber-preprocessor/esbuild').createEsbuildPlugin

const createBundler = require('@bahmutov/cypress-esbuild-preprocessor')
const nodePolyfills =
    require('@esbuild-plugins/node-modules-polyfill').NodeModulesPolyfillPlugin

const addCucumberPreprocessorPlugin =
    require('@badeball/cypress-cucumber-preprocessor').addCucumberPreprocessorPlugin

module.exports = async (on, config) => {
    await addCucumberPreprocessorPlugin(on, config) // to allow json to be produced
    // To use esBuild for the bundler when preprocessing
    on(
        'file:preprocessor',
        createBundler({
            plugins: [nodePolyfills(), createEsbuildPlugin(config)],
        })
    )
    return config
}

cypress.config.js

const { defineConfig } = require('cypress')
const selectTestsWithGrep = require('cypress-select-tests/grep')
const allureWriter = require("@shelex/cypress-allure-plugin/writer");


module.exports = defineConfig({
  env: {
    baseurl: 'https://google.com',
    username: 'admin',
    password: 'admin',
  },
  video: false,
  screenshotsFolder: 'screenshots',
  chromeWebSecurity: false,
  defaultCommandTimeout: 45000,
  pageLoadTimeout: 90000,
  screenshotOnRunFailure: true,
  viewportWidth: 1400,
  viewportHeight: 900,
  e2e: {
    specPattern: ["**/*.feature", "cypress/e2e/**/*.cy.{js,jsx,ts,tsx}"],
    supportFile: false,
    // excludeSpecPattern: "*.js",
    setupNodeEvents(on, config) {
      //require('cypress-mochawesome-reporter/plugin')(on);
      on('file:preprocessor', selectTestsWithGrep(config));
      allureWriter(on, config);
      return config;
    }
  }
})

报错截图

报错截图


解决方案

核心问题是**cypress.config.js的setupNodeEvents未引入index.js中的Cucumber预处理逻辑,且重复注册了file:preprocessor事件**,导致配置冲突引发崩溃。

1. 修改cypress.config.js,合并所有插件逻辑

将Cucumber的预处理配置与grep、Allure插件整合,避免重复注册事件:

const { defineConfig } = require('cypress')
const selectTestsWithGrep = require('cypress-select-tests/grep')
const allureWriter = require("@shelex/cypress-allure-plugin/writer");
// 引入index.js的Cucumber预处理配置(路径根据实际项目结构调整)
const cucumberPreprocessor = require('./cypress/plugins/index.js');

module.exports = defineConfig({
  env: {
    baseurl: 'https://google.com',
    username: 'admin',
    password: 'admin',
  },
  video: false,
  screenshotsFolder: 'screenshots',
  chromeWebSecurity: false,
  defaultCommandTimeout: 45000,
  pageLoadTimeout: 90000,
  screenshotOnRunFailure: true,
  viewportWidth: 1400,
  viewportHeight: 900,
  e2e: {
    specPattern: ["**/*.feature", "cypress/e2e/**/*.cy.{js,jsx,ts,tsx}"],
    supportFile: false,
    setupNodeEvents: async (on, config) => {
      // 先加载Cucumber的预处理配置
      config = await cucumberPreprocessor(on, config);
      
      // 整合grep插件,基于Cucumber的bundler处理文件
      on('file:preprocessor', (file) => {
        const processedFile = config.filePreprocessor(file);
        return selectTestsWithGrep(config)(file, processedFile);
      });
      
      // 保留Allure插件配置
      allureWriter(on, config);
      return config;
    }
  }
})

2. 确保依赖版本兼容

  • @badeball/cypress-cucumber-preprocessor版本需与Cypress版本匹配(例如Cypress 12+对应preprocessor 12.x及以上)
  • @bahmutov/cypress-esbuild-preprocessor需适配当前使用的Node.js版本

3. 清理依赖缓存重新安装

rm -rf node_modules package-lock.json
npm install

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 07:35:21