Implementing TypeScript module resolution with Cucumber-js can be a bit tricky

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.

Answer №1

One thing that immediately catches my attention is that in your cucumber profile, you are referencing the .ts files instead of the compiled TypeScript files that cucumber-js typically looks for. You may want to try this updated configuration:

const common = [
  "features/**/*.feature",
  "--require-module ts-node/register",
  // "--require-module tsconfig-paths/register",
  "--require step-definitions/**/*.js",
  "--require support/**/*.js"
].join(" ");

Additionally, while it might not be a critical issue, I always make sure to specify the file type at the end of my includes in the tsconfig file.

I've actually been considering creating an example cucumber-js project using TypeScript and sharing it on GitHub for others to use. Perhaps now is the time for me to finally do so!

Answer №2

While not a definitive solution to the issue at hand, I have found success using @cucumber/cucumber version 7.x. It's likely that this update has also been applied to previous stable releases.

For those interested, you can find a functional example project for testing purposes here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for instructing the Typescript compiler to process JSX within .ts files?

My .ts files contain .jsx syntax, and I am looking to instruct tsc on how to compile them the way it compiles .tsx files. Is there a way to adjust the configuration of tsc to achieve this? Additionally, are there steps to configure vscode for proper synt ...

The validation using regex is unsuccessful when the 6th character is entered

My attempt at validating URLs is encountering a problem. It consistently fails after the input reaches the 6th letter. Even when I type in "www.google.com," which is listed as a valid URL, it still fails to pass the validation. For example: w ww www ww ...

Discovering the generic type from an optional parameter within a constructor

Looking to implement an optional parameter within a constructor, where the type is automatically determined based on the property's type. However, when no argument is provided, TypeScript defaults to the type "unknown" rather than inferring it as "und ...

How can I retrieve the decimal x and y coordinates when the mouse is moved in Typescript Angular?

I am in the process of transitioning my user interface from Flash/Flex, where it stores values in decimal format. I need to access and reuse these values. Here is a demo showcasing my problem. Here is a snippet: import { Component, Input } from '@an ...

Create a tuple type by mapping an object with generics

I have a specified object: config: { someKey: someString } My goal is to generate a tuple type based on that configuration. Here is an example: function createRouter< T extends Record<string, string> >(config: T) { type Router = { // ...

Angular selects the initial three arrays out of an array

In my code, I have a collection of arrays that are nested within another array. My task is to extract the first three arrays from this collection. For instance, consider the following example: [{[1]},{[2]},{[3]},{[4]}] I apologize for not presenting a p ...

Contrast between utilizing and publishing, demanding and bringing in within Express

I have recently started learning Typescript and Express. I have a simple exported function that looks like this: export function testFunction(req: any, res: any) { console.log(req.body); return res.status(200).send('OK'); }; And ...

The mat-table component in my HTML code is not displaying the dataSource as expected, even though I meticulously copied it from Material Angular

Although I am aware that my question may seem unusual, my issue precisely matches what the title conveys. The problem lies in my mat-table dataSource not displaying any data, even after attempting to log the data with console.log("My Data : ", this.dataSou ...

Introduce a specialized hierarchical data structure known as a nested Record type, which progressively ref

In my system, the permissions are defined as an array of strings: const stringVals = [ 'create:user', 'update:user', 'delete:user', 'create:document', 'update:document', 'delete:document&ap ...

Tips for utilizing ngIf based on the value of a variable

Here is the code from my file.html: <button ion-button item-right> <ion-icon name="md-add-circle" (click)="save();"></ion-icon> </button> The content of file.ts is: editmode = false; I am trying to achieve the foll ...

Executing a service prior to the loading of Angular 7 applications or components

Currently, I am in the process of developing an application using Angular 7. So far, everything is running smoothly as I have successfully managed API calls, JWT Token authentication with C#, and updating LocalStorage when needed during user login and logo ...

Is there a way to modify the button exclusively within the div where it was pressed?

I plan to incorporate three buttons in my project (Download, Edit, and Upload). Upon clicking the Download button, a function is triggered, changing the button to Edit. Clicking the Edit button will then change it to Upload, and finally, clicking the Uplo ...

What is the proper way to integrate helmet.js with typescript?

Utilizing helmet from pure JavaScript according to the documentation is quite straightforward: const express = require('express') const helmet = require('helmet') const app = express() app.use(helmet()) However, I'm unsure how ...

All components load successfully except for the worker in Webpack

I've been working on configuring webpack to bundle my react project. I successfully set up the webpack_public_path variable and all modules are loading correctly, except for the worker. const path = require('path'); const MonacoWebpackPlugi ...

What could be causing the error within the 'react-code-blocks' library?

Currently, I am involved in a project that utilizes Typescript and React. However, I have encountered an issue while working with the 'react-code-blocks' library. The structure of my container component is as follows: interface ICodeBlockProps { ...

Navigating to specific rows in a primeng virtualscroll table using the scrollToIndex

Utilizing Primeng virtual scroll table in Angular to manage large datasets in an array. I am interested in using the scrollToIndex. Is there an equivalent of cdk-virtual-scroll-viewport in Primeng table? I need the functionality where, upon a user clicking ...

Navigate back to the main page using a tab

Is there a way to navigate back to the rootPage that is defined in the appComponent when using tabs? I have found that the setRoot method does not function as I anticipated. When used on a Tab page, the navigation stack is not cleared. Instead of the navig ...

what kind of bespoke entity in TypeScript

Exploring typescript for the first time, I have constructed this object: const startingState = { name: { value: "", error: false }, quantity: { value: 0, error: false }, category: "Grocery& ...

Remix.js is unable to perform a redirect action when utilizing the Form component

I've been searching through the Remix documentation, but I can't seem to find a solution to my issue. It appears that you're unable to redirect from an action when using the Form component in Remix. You can check out this StackBlitz example ...

Guide to sending jQuery data back to main class in TypeScript

Hi everyone, I'm diving into the world of typescript and JQuery. I have a simple question. In my typescript class called DatePicker, I am calling a function. Here's a snippet of the code: Class DatePicker { private pickerData; public update( ...