-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add ESM support (take 2) #1649
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
Merged
Merged
add ESM support (take 2) #1649
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d268a8d
Revert "temporarily revert ESM change (#1647)"
davidjgoss e6ae1e7
add failing scenario for deep imports
davidjgoss 836b968
merge main
davidjgoss 0f46a86
merge main
davidjgoss ec04d2f
define entry point with dot
davidjgoss 16e4f2f
make deep imports work via export patterns
davidjgoss a3a0414
move doc to own file
davidjgoss ce73823
link to doc from readme
davidjgoss 3bc91ad
add changelog entry
davidjgoss 47a2c68
add example to doc
davidjgoss 6b8ffd9
remove confusing comment
davidjgoss 947b39f
remove cli option, use import by default
davidjgoss 5fa807c
update documentation
davidjgoss 2b59f6e
remove redundant describe
davidjgoss 5465783
fix ordering
davidjgoss 7dec8dc
Merge remote-tracking branch 'origin/main' into esm-take-2
davidjgoss 6d782de
Merge branch 'main' into esm-take-2
davidjgoss 7cfdd73
Update features/esm.feature
davidjgoss 0244b61
Update features/esm.feature
davidjgoss 148f4c4
Merge branch 'main' into esm-take-2
davidjgoss c71d5b9
simplify tagging
davidjgoss 809f5af
use import only if a javascript file
davidjgoss a870138
add note about no transpilers
davidjgoss 46125e2
inline to avoid confusing reassignment
davidjgoss ebbf550
whoops, re-add try/catch
davidjgoss 9fefa63
merge main
davidjgoss 8ece5f8
use require with transpilers; import otherwise
davidjgoss b3e81cc
remove pointless return
davidjgoss 1bf48db
Merge branch 'main' into esm-take-2
aurelien-reeves c457355
support .cjs config file
davidjgoss 6714025
Merge branch 'main' into esm-take-2
davidjgoss 8037766
type and import the importer
davidjgoss 83e513b
actually dont import - causes issues
davidjgoss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# ES Modules (experimental) | ||
|
||
You can optionally write your support code (steps, hooks, etc) with native ES modules syntax - i.e. using `import` and `export` statements without transpiling. This is enabled without any additional configuration, and you can use either of the `.js` or `.mjs` file extensions. | ||
|
||
Example (adapted from [our original example](./nodejs_example.md)): | ||
|
||
```javascript | ||
// features/support/steps.mjs | ||
import { Given, When, Then } from '@cucumber/cucumber' | ||
import { strict as assert } from 'assert' | ||
|
||
Given('a variable set to {int}', function (number) { | ||
this.setTo(number) | ||
}) | ||
|
||
When('I increment the variable by {int}', function (number) { | ||
this.incrementBy(number) | ||
}) | ||
|
||
Then('the variable should contain {int}', function (number) { | ||
assert.equal(this.variable, number) | ||
}) | ||
``` | ||
|
||
As well as support code, these things can also be in ES modules syntax: | ||
|
||
- Custom formatters | ||
- Custom snippets | ||
|
||
You can use ES modules selectively/incrementally - so you can have a mixture of CommonJS and ESM in the same project. | ||
|
||
When using a transpiler for e.g. TypeScript, ESM isn't supported - you'll need to configure your transpiler to output modules in CommonJS syntax (for now). | ||
|
||
The config file referenced for [Profiles](./profiles.md) can only be in CommonJS syntax. In a project with `type=module`, you can name the file `cucumber.cjs`, since Node expects `.js` files to be in ESM syntax in such projects. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
@esm | ||
Feature: ES modules support | ||
|
||
cucumber-js works with native ES modules | ||
|
||
Scenario Outline: native module syntax works in support code, formatters and snippets | ||
Given a file named "features/a.feature" with: | ||
""" | ||
Feature: | ||
Scenario: one | ||
Given a step passes | ||
|
||
Scenario: two | ||
Given a step passes | ||
""" | ||
And a file named "features/step_definitions/cucumber_steps.js" with: | ||
""" | ||
import {Given} from '@cucumber/cucumber' | ||
|
||
Given(/^a step passes$/, function() {}); | ||
""" | ||
And a file named "custom-formatter.js" with: | ||
""" | ||
import {SummaryFormatter} from '@cucumber/cucumber' | ||
|
||
export default class CustomFormatter extends SummaryFormatter {} | ||
""" | ||
And a file named "custom-snippet-syntax.js" with: | ||
""" | ||
export default class CustomSnippetSyntax { | ||
build(opts) { | ||
return 'hello world' | ||
} | ||
} | ||
""" | ||
And a file named "cucumber.cjs" with: | ||
""" | ||
module.exports = { | ||
'default': '--format summary' | ||
} | ||
""" | ||
When I run cucumber-js with `<options> --format ./custom-formatter.js --format-options '{"snippetSyntax": "./custom-snippet-syntax.js"}' <args>` | ||
Then it passes | ||
Examples: | ||
| args | | ||
| | | ||
| --parallel 2 | | ||
|
||
Scenario: .mjs support code files are matched by default | ||
Given a file named "features/a.feature" with: | ||
""" | ||
Feature: | ||
Scenario: | ||
Given a step passes | ||
""" | ||
And a file named "features/step_definitions/cucumber_steps.mjs" with: | ||
""" | ||
import {Given} from '@cucumber/cucumber' | ||
|
||
Given(/^a step passes$/, function() {}); | ||
""" | ||
When I run cucumber-js | ||
Then it passes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.