我正在做一个小的概念验证Cucumber-js-使用TypeScript。到目前为止,一切都是fine...but,我找不到一种方法来正确地配置模块解析当使用tsconfig-paths。
这是我的项目结构:
.
├── 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);
}
...
}我不能使用...but@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. (/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 ()任何精通TypeScript和/或黄瓜的人都可以解释一下这里发生了什么?
更新如果我删除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)它是有效的:令人兴奋的:另外,更改baseUrl到其他东西,而不是"./"例如,"./support"
,不删除cucumber.js成功了。
发布于 2021-02-19 07:32:02
这个问题不是一个真正的解决方案,但它目前正在与
版本@cucumber/cucumber version 7.x-很可能更改被反向移植到以前的稳定版本。这里是一个有效的(类似模板的项目)来测试这一点。
发布于 2019-08-06 01:28:01
我看到的第一件事是在您的cucumber配置文件中,您引用的是ts文件,而不是我相信cucumber-js要查找的编译后的typescript。尝试:
const common = [
"features/**/*.feature",
"--require-module ts-node/register",
// "--require-module tsconfig-paths/register",
"--require step-definitions/**/*.js",
"--require support/**/*.js"
].join(" ");此外,我不认为这是一个问题,但我总是将文件类型添加到我在tsconfig文件中包含的文件的末尾。
我一直计划将一个带有typescript的示例cucumber-js项目放到github上,供人们使用,所以这可能是我需要的动机:)
https://stackoverflow.com/questions/57343544
复制相似问题