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

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
}

排查与解决步骤

  1. 修复配置文件的转义字符问题
    配置中--format-options参数使用了HTML实体",Cucumber无法解析该字符,需替换为实际双引号:

    let common=[
        'features/login.feature',
        '--format-options {"snippetInterface":"async-await"}',
        '--publish-quiet'
    ].join(' ');
    
    module.exports={
        default:common
    }
    
  2. 检查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';
    });
    
  3. 验证文件路径与位置

    • 确认features/login.feature文件真实存在,路径和文件名大小写与配置一致
    • Step Definition文件需放置在Cucumber默认扫描路径下(如features/step_definitions目录),或在配置中通过--require参数指定路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 11:47:28