gpt4 book ai didi

TypeScript 模块解析不适用于 Cucumber-js

转载 作者:行者123 更新时间:2023-12-05 07:20:46 25 4
gpt4 key购买 nike

我正在为 Cucumber-js 做一个小的概念验证——使用 TypeScript。到目前为止一切都很好......但是我找不到在使用 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 – 阅读:在 cucumber.js 中取消注释 "--require-module tsconfig-paths/register" ,我收到此错误消息:

$ 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") 它有效:令人兴奋:

此外,将 baseUrl 更改为 "./" 以外的内容,例如 "./support",而不删除 >cucumber.js 可以解决问题。

最佳答案

我看到的第一件事是在你的 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 上供人们使用,所以也许这就是我需要的动力:)

关于TypeScript 模块解析不适用于 Cucumber-js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com