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

TypeScript Module Resolution doesn't quite work with Cucumber-js

我正在为 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,所以 "step definitions" 看起来像:

// 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.jsoncucumber.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,
};

一旦我 "activate" 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 的人and/or 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 即可。

我看到的第一件事是在你的黄瓜配置文件中你引用的是 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 的示例 cucumber-js 项目放到 github 上供人们使用,所以也许这就是我需要的动力:)

并不是真正解决此问题的答案,但目前正在使用 @cucumber/cucumber 版本 7.x — 并且可能更改已移植到以前的稳定版本。

Here 是一个测试这个的工作(类似模板的项目)。