Currently, I am in the process of creating a Proof of Concept for Cucumber-js using TypeScript. Everything is going smoothly except for one issue - I am facing difficulties when it comes to configuring the module resolution while utilizing tsconfig-paths
.
This is the structure of 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
I am using cucumber-tsflow
, hence my "step definitions" appear as follows:
// 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);
}
...
}
Despite correctly configuring @model/bank-account.model
in both tsconfig.json
and cucumber.js
, I am unable to utilize it:
# 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,
};
Once I include
"--require-module tsconfig-paths/register"
in cucumber.js
, an error message pops up:
$ 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>)
I would greatly appreciate any insights from those experienced with TypeScript or Cucumber regarding this issue.
UPDATE
Interestingly, by removing cucumber.js
and passing all options directly through the npm
script (
"test": "cucumber-js features/**/*.feature --require-module ts-node/register --require-module tsconfig-paths/register --require step-definitions/**/*.ts --require support/**/*.ts"
), everything works flawlessly :mind-blowing:
Furthermore, changing the baseUrl
to something different, such as "./support"
instead of "./"
, also resolves the issue without the need to delete cucumber.js
.