-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native JS module support in step definitions and support code #1304
Comments
I would like to get cucumber-js working with ESM. We had some other issues opened about it already. We could make a CLI option (or use some other way to look for when to user it) that makes cucumber-js use Given the example you added, we could add a test case specifically for this and work on making it pass. Experimenting with switching require to import and waiting on the promise I then ran into another error:
|
I took the example (slightly modified) from the step definition documentation, but you're right, it won't work as written. Because Cucumber.js is a CommonJS module, it only provides a default export. In the short term, I would probably just import the module default and then access the Given/When/Then methods from that:
Longer term, it would be nice if Cucumber had separate entry points for import and require, as shown here. Thanks for looking at this. For what it's worth, a CLI option to tell Cucumber to import instead of requiring would suit me fine. It would also be great if Cucumber would look for support files with .cjs and .mjs extensions, and automatically treat those as CommonJS and native modules respectively. EDIT: just want to reiterate that I'm happy to help with any of this if I can; just point me in the right direction. |
This doesn't work. I get the same message. If I use either of these:
I get the same error message as the original post.
Then I get this message:
If I rename UUUUUU
Failures:
1) Scenario: Empty Payload # spec\cucumber\features\users\create\main.feature:4
? When the client creates a POST request to /users
Undefined. Implement with the following snippet:
When('the client creates a POST request to \/users', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
... etc I get the exact same messages if I delete the file completely, so it's clearly being ignored. Solution (sort of)In the end, the only way I've been able to get cucumber to run in a project with "type": "module" is to create a separate spec\package.json without "type": "module" and install cucumber into So far it seems to be working. Let's see if it's still working once I've created some actual tests. To the maintainers: this is my first time using cucumber, and I like it so far. However, this has really put a dent in my progress. Instead of spending the afternoon learning cucumber I spent it debugging imports. I know how annoying it is trying to create a library that support both ES6 imports and CJS so you have my sympathy. A possible solution: dist/cucumber.js Then add this line to package.json: "module": "dist/cucumber.module.js" This should enable node to resolve ES6 imports. |
there has to be a better way to do this. I too am having that issue, and because imports cant be used with commonJS I would have to use everywhere... is there a way that Cucumber can have a flag to help enable us to use ES? |
Quick one for folks interested in this: when you use ESM in a project with cucumber, would you:
Thanks! |
I tend to follow the convention of using .mjs and .cjs extensions for all my JavaScript files to avoid ambiguity, and it would be my preference to do so in this case as well. However, it would be no major inconvenience to set “type”: “module” at the project level and leave these as .js files if that’s the better solution for more users. |
I use the |
Thanks for the feedback @adamlacoste @looeee |
@davidjgoss does #1589 being merged is enough for that issue to be closed? Or do we need something else? |
Thanks for the nudge @aurelien-reeves! Version 7.2.0 of If those interested could give it a try and report what you find that would be great. I put a super minimal example project up here: Happy to keep this issue open for a bit just to catch the early feedback in one place. |
Refs. #1646 |
So this change caused an issue with third-party frameworks and formatters that |
i've been attempting to give in order to use
since that option wasn't available, i then attempted with
for reference, this was against node it would be nice if this worked with the is there an approach to defining a loader that you would expect to work with |
@davidjgoss it appears that this error still occurs when attempting to use a loader with |
Would you have a minimal reproducible example? |
Also, would you agree opening a dedicated issue for that? |
appologies for the delay, but created a feature request at #1844 |
Problem
When I attempt to run Cucumber.js with a step definition defined in a native ECMAScript module (as documented here), the attempt fails with a warning that I am using CommonJS's
require()
on an ES module.Cucumber.js version: 6.0.5
Node version: 13.8.0
Steps to reproduce
Setup a basic NPM package directory using
npm init
andnpm i cucumber
"type": "module"
in thepackage.json
file to ensure that JS files will be treated as native modulesCreate a basic feature file,
features/mjs.feature
:Create a basic step definition file,
features/step_definitions.js
:Attempt to run Cucumber:
Expected result
The step definition module should be loaded and used to parse the step.
Actual result
Closing
For what it's worth, I really appreciate the work that the Cucumber.js team has put in on this project, it has been a major asset to me and my company. My team has invested some time in building some shared application components in native JS modules, and until recently I had been assuming (based on the
import
syntax in the most current documentation) that this stuff would work with Cucumber when we got around to integrating with our test framework. That appears not to be the case, though. Since the shared component is already written and integrated into other parts of our application, I'm reluctant use only step definitions and support code written in CommonJS because of interoperability challenges.Other things I attempted to get this to work included...
.mjs
extensions, per Node.js convention (this caused them to be overlooked by Cucumber's auto-loading logic, and using--require
to explicitly load the step definition module threwError [ERR_REQUIRE_ESM]: Must use import to load ES Module
import()
function; the problem was that this approach is asynchronous, and even if the import promise is exported by the CJS module, Cucumber doesn't appear to wait for that promise to resolve before proceeding, because the step was marked as undefined (suggesting that the step definition in the native module was not yet registered).If it's within my ability, I'd be happy to submit a PR to help resolve this issue, but I'm unfamiliar with the internal workings of Cucumber.js and would appreciate if someone could point me in the right direction.
The text was updated successfully, but these errors were encountered: