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

在Deno中运行Cucumber的实现方案咨询

在Deno中使用CucumberJS的实践方案

CucumberJS本身是Node.js生态工具,直接在Deno中使用确实存在适配问题,以下是两种经过验证的可行方案,以及针对你遇到的配置加载、TS步骤无法运行问题的解决办法:

方案一:借助Deno的Node兼容层运行CucumberJS

这是最贴近你原有CucumberJS使用习惯的方案:

  • 临时创建package.json管理Cucumber依赖:
    {
      "devDependencies": {
        "@cucumber/cucumber": "^9.0.0",
        "typescript": "^5.0.0"
      }
    }
    
  • 执行npm install安装依赖
  • 编写cucumber.js配置文件,指定用Deno处理TS代码:
    module.exports = {
      default: {
        paths: ['features/**/*.feature'],
        require: ['steps/**/*.ts'],
        format: ['progress'],
        requireModule: ['ts-node/register'],
        execArgv: ['--deno', '--allow-all']
      }
    };
    
  • 步骤定义文件中,Node API的导入改为Deno兼容写法,比如import * as fs from 'node:fs'
  • 运行测试:deno run --allow-read --allow-env npm:@cucumber/cucumber

方案二:使用Deno原生Cucumber替代库

如果不想依赖Node生态,可直接用Deno社区的原生实现deno-cucumber:

  • 步骤文件中导入库:
    import { Given, When, Then } from "https://deno.land/x/cucumber/mod.ts";
    
  • 编写.feature和步骤定义的语法与CucumberJS完全一致
  • 运行命令:deno run --allow-read --allow-run https://deno.land/x/cucumber/cli.ts features/

解决你遇到的具体问题

配置文件加载异常

如果用编程方式启动CucumberJS,需适配Deno的ES模块规则:

  • 将配置改为ES模块格式(比如cucumber.mjs),或在package.json中添加"type": "module"
  • 编程启动代码示例:
    import { runCucumber } from "npm:@cucumber/cucumber";
    
    await runCucumber({
      paths: ["features/**/*.feature"],
      require: ["steps/**/*.ts"],
      format: ["progress"],
      requireModule: ["ts-node/register"],
    });
    
  • 执行命令:deno run --allow-all --node your-runner.ts

TypeScript步骤无法直接使用

  • 确保tsconfig.json适配Deno:
    {
      "compilerOptions": {
        "module": "ESNext",
        "target": "ESNext",
        "allowJs": true,
        "strict": true
      },
      "include": ["steps/**/*.ts"]
    }
    
  • 步骤文件避免使用Node专属语法(比如__dirname),替换为Deno的Deno.cwd()
  • 运行时必须加上--node参数,让Deno启用Node模块解析能力

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 06:13:12