Saturday 23 October 2021

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?



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

No comments:

Post a Comment