Cypress v13.4.0配置cypress-cucumber-preprocessor遇类型错误
解决cypress-cucumber-preprocessor配置中的TypeError: createBundler is not a function错误
错误原因
你遇到的TypeError: (0 , cypress_esbuild_preprocessor_1.default) is not a function错误,核心原因是**@bahmutov/cypress-esbuild-preprocessor v2.2.0版本对导出方式做了变更**:原本的createBundler是默认导出,现在改为了命名导出,而你的代码仍在使用默认导入的方式,导致无法正确加载该函数。
解决方案
1. 修正createBundler的导入方式
将配置文件中的默认导入改为命名导入:
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: { specPattern: "**/*.feature", async setupNodeEvents(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions): Promise<Cypress.PluginConfigOptions> { await addCucumberPreprocessorPlugin(on, config); on("file:preprocessor", createBundler({plugins: [createEsbuildPlugin(config)]})); return config; }, }, })
2. 完善tsconfig.json配置
确保你的tsconfig.json配置与Node.js的模块系统兼容,补充moduleResolution选项(必须与module值一致):
{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext", "target": "ES2020", "esModuleInterop": true, "skipLibCheck": true, "strict": true }, "include": ["cypress.config.ts", "cypress/**/*"] }
3. 重新安装依赖
为了避免缓存导致的问题,执行以下操作:
- 删除项目根目录下的
node_modules文件夹 - 删除
package-lock.json或yarn.lock文件 - 重新执行
npm install或yarn install
验证
完成上述步骤后,启动Cypress,检查错误是否消失,feature文件是否能正常加载执行。
内容的提问来源于stack exchange,提问作者user22443312
相关产品推荐
相关产品推荐

