Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Cucumber with my ESM (ECMA Module Loader) Project?

I have a simple project that is basically...

export class Application{
  constructor(...){
    ...
  }
  async run(){
    console.log("I Ran!");
  }
}

I want to run this using Cukes so I follow these steps and get this working (notice the .cjs extension to signify to node it is a cjs file)

// features/basic.feature

Feature: Hello World
  Scenario: Hello World
    Given I start the app
    When it is running
    Then I see the console

// features/support/steps.cjs
const { Given, When, Then } = require("@cucumber/cucumber");

Given("I start the app", function () {
  // TODO: Setup child process
  return
});

When("it is running", function () {
  // TODO: Execute using Worker
  return
});

Then("I see the console", function () {
  // assert.equal(this.variable, number);
  return
});

I would execute this using cucumber-js --require features/support/steps.cjs

But now I want to import Application and run the application in a step. Since I can't import a ESM (.mjs) file using .cjs I am not sure how to do this. I tried creating a .mjs version of the step file but I can't get that working either. I also tried cucumber-js --require-module features/support/steps.mjs but it still didn't work.

How do I use Cukes with an ESM-style project?

like image 437
Jackie Avatar asked Oct 18 '21 20:10

Jackie


People also ask

Can you use ESM in node?

ECMAScript modules, also known as ESM, is the official standard format to package JavaScript, and fortunately Node. js supports it 🎉.

How do you use ESM modules in CommonJS?

Importing from ESM and CommonJS to CommonJS Since require() is synchronous, you can't use it to import ESM modules at all! In CommonJS you have to use require syntax for other CommonJS modules and an import() function (distinct from the import keyword used in ESM!), a function that returns a promise, to import ESM.


1 Answers

As far as I know, this is still an issue. There is an experimental version of cukes to work with, which started addressing these issues (cukes version 7.2.0), that you could start using to see if you can address the changes you want to implement.

However, version 8.0.0 is on it's way which has a lot of these issues addressed in particular. The new release is due out in the next few days (fingers crossed).

Here's the link for all the work they have done to date to address the issues you are experiencing: cucumber-js add ESM support (take 2)

like image 135
djmonki Avatar answered Oct 20 '22 21:10

djmonki