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

Cypress从8.7.0迁移至10.10.0时触发全局注册表错误

Cypress 10迁移后cucumber预处理器报错问题

环境版本

  • Cypress 10.10.0
  • @badeball/cypress-cucumber-preprocessor: ^11.4.0
  • node v18.4.0
  • @bahmutov/cypress-esbuild-preprocessor: ^2.1.5

报错信息

Expected to find a global registry (this usually means you are trying to define steps or hooks in support/e2e.js, which is not supported) (this might be a bug, please report at https://github.com/badeball/cypress-cucumber-preprocessor)

Because this error occurred during a before each hook we are skipping all of the remaining tests.

详细错误日志:

Because this error occurred during a `before each` hook we are skipping all of the remaining tests.
    at fail (tests?p=tests/cypress/e2e/login/loginBase.feature:964:15)
    at assert (tests?p=tests/cypress/e2e/login/loginBase.feature:971:9)
    at assertAndReturn (tests?p=tests/cypress/e2e/login/loginBase.feature:975:9)
    at getRegistry (tests?

已做操作

已在e2e.js和support/index.js中添加错误处理,但问题未解决。项目根目录有.env环境变量文件。

相关代码文件

测试用例:tests/cypress/e2e/login/login.feature

@regression
@login
Feature: Login to base url
  
  Scenario: Login to base url
    Given I go to base url

步骤定义:tests/cypress/stepDefinitions/login.cy.js

import { Given, When, Then, Before, After, And } from "@badeball/cypress-cucumber-preprocessor";

When('I go to base url', () => {
  cy.visit(Cypress.config().baseUrl);
})

tests/cypress/support/index.js

// Import commands.js using ES2015 syntax:
import './commands'
Cypress.on('uncaught:exception', (err, runnable) => {
  // returning false here prevents Cypress from
  // failing the test
  return false
});

tests/cypress/support/e2e.js

// Import commands.js using ES2015 syntax:
import './commands'
 
Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
    return false
})

.cypress-cucumber-preprocessorrc.json(项目根目录)

{
  "stepDefinitions": [
    "[filepath].{js,ts}",
    "tests/cypress/stepDefinitions/**/*.{js,ts}"
  ]
}

cypress.config.js

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").createEsbuildPlugin;
const dotenvPlugin = require('cypress-dotenv');

async function setupNodeEvents(on, config) {
    await addCucumberPreprocessorPlugin.addCucumberPreprocessorPlugin(on, config);
    on(
        "file:preprocessor",
        createBundler({
            plugins: [createEsbuildPlugin(config)],
        })
    );
    //webpack config goes here if required
    config = dotenvPlugin(config)
    return config;
}

module.exports = defineConfig({
    e2e: {
        baseUrl: 'https://bookmain.co',
        apiUrl: 'https://bookmain.co/api/books/',
        specPattern: "tests/cypress/e2e/**/*.feature",
        supportFile: false,
        setupNodeEvents
    },
    component: {
        devServer: {
            framework: "next",
            bundler: "webpack",
        },
    },
});

package.json中的配置

"cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true,
    "stepDefinitions": "tests/cypress/stepDefinitions/**/*.{js,ts}",
    "cucumberJson": {
      "generate": true,
      "outputFolder": "tests/cypress/cucumber-json",
      "filePrefix": "",
      "fileSuffix": ".cucumber"
    }
},

解决方案

1. 修复supportFile配置问题

cypress.config.js中设置了supportFile: false,但实际存在support/e2e.js文件,这会导致预处理器无法正确识别全局配置,进而找不到注册表。修改配置指向正确的支持文件:

module.exports = defineConfig({
    e2e: {
        // ...其他配置
        supportFile: "tests/cypress/support/e2e.js", // 替换为实际路径
        // ...其他配置
    },
});

2. 统一配置优先级,移除重复配置

同时在package.json和.cypress-cucumber-preprocessorrc.json中配置stepDefinitions会引发冲突,建议删除package.json中的cypress-cucumber-preprocessor配置块,保留.cypress-cucumber-preprocessorrc.json中的配置即可。

3. 检查步骤定义文件命名规范

确保步骤定义文件命名符合预处理器的匹配规则,当前login.cy.js的命名没问题,但需确认.cypress-cucumber-preprocessorrc.json中的路径匹配正确。

4. 验证环境变量加载

若.env文件未被正确加载,可在setupNodeEvents中明确指定路径:

config = dotenvPlugin(config, { path: './.env' });

5. 清理缓存并重启Cypress

执行以下命令清理缓存后重新启动测试:

npx cypress cache clear
npx cypress open

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 15:25:16