我正在使用TypeScript为 Cucumber-js 做一个小的概念证明。到目前为止,一切都很好...但是在使用tsconfig-paths
时,我找不到正确配置模块分辨率的方法。
这是my project结构:
.
├── cucumber.js
├── features
│ └── bank-account.feature
├── package.json
├── package-lock.json
├── step-definitions
│ └── bank-account.steps.ts
├── support
│ └── model
│ └── bank-account.model.ts
├── tsconfig.json
└── tslint.json
我正在使用cucumber-tsflow
,因此“步骤定义”如下:
// import { BankAccount } from "@model/bank-account.model"; // ...trouble!
import { BankAccount } from "../support/model/bank-account.model";
import { expect } from "chai";
import { binding, given, then, when } from "cucumber-tsflow";
@binding()
export class BankAccountSteps {
...
@when("{int} is deposited")
public when(amount: number) {
this.account.deposit(amount);
}
...
}
...但是即使我在@model/bank-account.model
和tsconfig.json
中正确配置了cucumber.js
,我也无法使用它:
# file: tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
...
"paths": {
"*": ["./*"],
"@model/*": ["./support/model/*"]
},
...
"types": [
"@types/chai",
"@types/cucumber",
"node"
]
},
"exclude": ["node_modules/"],
"include": ["./step-definitions/**/*", "./support/**/*"]
}
# file: cucumber.js
const common = [
"features/**/*.feature",
"--require-module ts-node/register",
// "--require-module tsconfig-paths/register",
"--require step-definitions/**/*.ts",
"--require support/**/*.ts"
].join(" ");
module.exports = {
default: common,
};
我“激活” tsconfig-paths
–读取:在"--require-module tsconfig-paths/register"
内取消注释cucumber.js
,我收到以下错误消息:
$ npm run test
> cucumber-js --profile default
TypeError: cucumber_1.Before is not a function
at _.once (/home/x80486/Workshop/Development/cucumber-basics/node_modules/cucumber-tsflow/src/binding-decorator.ts:78:3)
at /home/x80486/Workshop/Development/cucumber-basics/node_modules/underscore/underscore.js:957:21
at /home/x80486/Workshop/Development/cucumber-basics/node_modules/cucumber-tsflow/src/binding-decorator.ts:38:5
at __decorate (/home/x80486/Workshop/Development/cucumber-basics/step-definitions/bank-account.steps.ts:5:95)
at Object.<anonymous> (/home/x80486/Workshop/Development/cucumber-basics/step-definitions/bank-account.steps.ts:8:30)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Module.m._compile (/home/x80486/Workshop/Development/cucumber-basics/node_modules/ts-node/src/index.ts:473:23)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/x80486/Workshop/Development/cucumber-basics/node_modules/ts-node/src/index.ts:476:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at supportCodePaths.forEach.codePath (/home/x80486/Workshop/Development/cucumber-basics/node_modules/cucumber/lib/cli/index.js:142:42)
at Array.forEach (<anonymous>)
任何精通TypeScript和/或Cucumber的人都可以对这里发生的事情有所了解吗?
更新
如果我删除cucumber.js
并仅通过npm
脚本("test": "cucumber-js features/**/*.feature --require-module ts-node/register --require-module tsconfig-paths/register --require step-definitions/**/*.ts --require support/**/*.ts"
)中的所有选项,它将起作用:mind-blowing:
此外,在不删除baseUrl
的情况下将"./"
更改为"./support"
以外的其他内容(例如cucumber.js
即可达到目的。
答案 0 :(得分:0)
我首先看到的是出现在我的黄瓜个人资料中的内容,您引用的是ts文件,而不是我相信Cucumber-js正在寻找的已编译的打字稿。试试:
const common = [
"features/**/*.feature",
"--require-module ts-node/register",
// "--require-module tsconfig-paths/register",
"--require step-definitions/**/*.js",
"--require support/**/*.js"
].join(" ");
此外,我不认为这是一个问题,但我总是将文件类型添加到tsconfig文件中包含文件的末尾。
我一直在计划将一个带有typescript的cumber-js示例项目放到github上供人们使用,所以这也许是我需要的动力:)
答案 1 :(得分:0)
并不是解决此问题的真正答案,但目前正在使用 @cucumber/cucumber
版本 7.x
- 并且可能更改已向后移植到以前的稳定版本。
Here 是一个可以测试这个的工作(类似模板的项目)。