在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
相关产品推荐
相关产品推荐

