Node.js中Cucumber的Scenario和Steps出现未定义错误求助
Cucumber.js项目运行抛出'undefined'错误的解决方法
在Eclipse中创建的Cucumber.js项目运行时抛出'undefined'错误,项目目录结构及错误截图见附图。相关代码如下:
Feature文件代码
@tag Feature: Title of your feature @tag1 Scenario: Title of your scenario Given I want to write a step with precondition And some other precondition
Step Definition代码
import {Given,And} from '@cucumber/cucumber' Given('/^I want to write a step with precondition$/', async function () { // Write code here that turns the phrase above into concrete actions return 'pending'; }); And('/^some other precondition$/', async function () { // Write code here that turns the phrase above into concrete actions return 'pending'; });
Cucumber.js配置代码
let common=[ 'features/login.feature', '--format-options {"snippetInterface":"async-await"}', '--publish-quiet' ].join(' '); module.exports={ default:common }
排查与解决步骤
修复配置文件的转义字符问题
配置中--format-options参数使用了HTML实体",Cucumber无法解析该字符,需替换为实际双引号:let common=[ 'features/login.feature', '--format-options {"snippetInterface":"async-await"}', '--publish-quiet' ].join(' '); module.exports={ default:common }检查ES模块兼容性
如果项目使用import语法(ES模块),需确保:- package.json中添加
"type": "module"字段 - Cucumber版本支持ES模块(建议使用较新版本)
若不想配置ES模块,可改用CommonJS语法:
const {Given,And} = require('@cucumber/cucumber'); Given(/^I want to write a step with precondition$/, async function () { return 'pending'; }); And(/^some other precondition$/, async function () { return 'pending'; });- package.json中添加
验证文件路径与位置
- 确认
features/login.feature文件真实存在,路径和文件名大小写与配置一致 - Step Definition文件需放置在Cucumber默认扫描路径下(如
features/step_definitions目录),或在配置中通过--require参数指定路径
- 确认
内容的提问来源于stack exchange,提问作者Tony
相关产品推荐
相关产品推荐

