Skip to content
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

add ESM support (take 2) #1649

Merged
merged 33 commits into from Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d268a8d
Revert "temporarily revert ESM change (#1647)"
davidjgoss Apr 21, 2021
e6ae1e7
add failing scenario for deep imports
davidjgoss Apr 22, 2021
836b968
merge main
davidjgoss Jun 20, 2021
0f46a86
merge main
davidjgoss Jul 1, 2021
ec04d2f
define entry point with dot
davidjgoss Jul 1, 2021
16e4f2f
make deep imports work via export patterns
davidjgoss Jul 1, 2021
a3a0414
move doc to own file
davidjgoss Jul 1, 2021
ce73823
link to doc from readme
davidjgoss Jul 1, 2021
3bc91ad
add changelog entry
davidjgoss Jul 1, 2021
47a2c68
add example to doc
davidjgoss Jul 2, 2021
6b8ffd9
remove confusing comment
davidjgoss Jul 2, 2021
947b39f
remove cli option, use import by default
davidjgoss Jul 3, 2021
5fa807c
update documentation
davidjgoss Jul 8, 2021
2b59f6e
remove redundant describe
davidjgoss Jul 8, 2021
5465783
fix ordering
davidjgoss Jul 8, 2021
7dec8dc
Merge remote-tracking branch 'origin/main' into esm-take-2
davidjgoss Jul 8, 2021
6d782de
Merge branch 'main' into esm-take-2
davidjgoss Jul 10, 2021
7cfdd73
Update features/esm.feature
davidjgoss Jul 12, 2021
0244b61
Update features/esm.feature
davidjgoss Jul 12, 2021
148f4c4
Merge branch 'main' into esm-take-2
davidjgoss Jul 28, 2021
c71d5b9
simplify tagging
davidjgoss Jul 28, 2021
809f5af
use import only if a javascript file
davidjgoss Jul 28, 2021
a870138
add note about no transpilers
davidjgoss Jul 28, 2021
46125e2
inline to avoid confusing reassignment
davidjgoss Jul 28, 2021
ebbf550
whoops, re-add try/catch
davidjgoss Jul 28, 2021
9fefa63
merge main
davidjgoss Sep 5, 2021
8ece5f8
use require with transpilers; import otherwise
davidjgoss Sep 5, 2021
b3e81cc
remove pointless return
davidjgoss Sep 5, 2021
1bf48db
Merge branch 'main' into esm-take-2
aurelien-reeves Sep 7, 2021
c457355
support .cjs config file
davidjgoss Sep 5, 2021
6714025
Merge branch 'main' into esm-take-2
davidjgoss Sep 16, 2021
8037766
type and import the importer
davidjgoss Sep 16, 2021
83e513b
actually dont import - causes issues
davidjgoss Sep 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,8 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO

### Added

* Add support for user code as native ES modules

### Changed

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -67,6 +67,7 @@ The following documentation is for master. See below for documentation for older
* [Attachments](/docs/support_files/attachments.md)
* [API Reference](/docs/support_files/api_reference.md)
* Guides
* [ES Modules](./docs/esm.md)
* [Running in parallel](./docs/parallel.md)
* [Retrying failing scenarios](./docs/retry.md)
* [Profiles](./docs/profiles.md)
Expand Down
1 change: 1 addition & 0 deletions dependency-lint.yml
Expand Up @@ -45,6 +45,7 @@ requiredModules:
- 'dist/**/*'
- 'lib/**/*'
- 'node_modules/**/*'
- 'src/importers.js'
- 'tmp/**/*'
root: '**/*.{js,ts}'
stripLoaders: false
Expand Down
15 changes: 15 additions & 0 deletions docs/esm.md
@@ -0,0 +1,15 @@
# 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.

To enable this, run with the `--esm` CLI option.

This will also expand the default glob for support files to include the `.mjs` file extension.

As well as support code, these things can also be in ES modules syntax:

- Custom formatters
- Custom snippets
- Your `cucumber.js` config file

You can use ES modules selectively/incrementally - the module loading strategy that the `--esm` flag activates supports both ES modules and CommonJS.
17 changes: 17 additions & 0 deletions features/direct_imports.feature
Expand Up @@ -44,3 +44,20 @@ Feature: Core feature elements execution using direct imports
"""
features/step_definitions/cucumber_steps.js:3
"""

Scenario: deep imports don't break everything
Given a file named "features/a.feature" with:
"""
Feature: some feature
Scenario: some scenario
Given a step passes
"""
And a file named "features/step_definitions/cucumber_steps.js" with:
"""
const {Given} = require('@cucumber/cucumber')
const TestCaseHookDefinition = require('@cucumber/cucumber/lib/models/test_case_hook_definition')

Given(/^a step passes$/, function() {});
"""
When I run cucumber-js
Then it passes
80 changes: 80 additions & 0 deletions features/esm.feature
@@ -0,0 +1,80 @@
Feature: ES modules support

cucumber-js works with native ES modules, via a Cli flag `--esm`
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved

@esm
Scenario Outline: native module syntax works when using --esm
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 "cucumber.js" with:
"""
export default {
'default': '--format message:messages.ndjson',
}
"""
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'
}
}
"""
When I run cucumber-js with `<options> --format ./custom-formatter.js --format-options '{"snippetSyntax": "./custom-snippet-syntax.js"}'`
Then it passes
Examples:
| options |
| --esm |
| --esm --parallel 2 |

@esm
Scenario: .mjs support code files are matched by default when using --esm
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
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 with `--esm`
Then it passes

Scenario: native module syntax doesn't work without --esm
Given a file named "features/a.feature" with:
"""
Feature:
Scenario:
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() {});
"""
When I run cucumber-js
Then it fails
14 changes: 13 additions & 1 deletion features/support/hooks.ts
Expand Up @@ -13,7 +13,7 @@ Before('@debug', function (this: World) {
this.debug = true
})

Before('@spawn', function (this: World) {
Before('@spawn or @esm', function (this: World) {
this.spawn = true
})

Expand Down Expand Up @@ -43,6 +43,18 @@ Before(function (
this.localExecutablePath = path.join(projectPath, 'bin', 'cucumber-js')
})

Before('@esm', function (this: World) {
const [majorVersion] = process.versions.node.split('.')
if (Number(majorVersion) < 12) {
return 'skipped'
}
fsExtra.writeJSONSync(path.join(this.tmpDir, 'package.json'), {
name: 'feature-test-pickle',
type: 'module',
})
return undefined
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
})

Before('@global-install', function (this: World) {
const tmpObject = tmp.dirSync({ unsafeCleanup: true })

Expand Down
12 changes: 10 additions & 2 deletions package.json
Expand Up @@ -163,6 +163,15 @@
"lib": "./lib"
},
"main": "./lib/index.js",
"exports": {
".": {
"import": "./lib/wrapper.mjs",
"require": "./lib/index.js"
},
"./lib/*": {
"require": "./lib/*.js"
}
},
"types": "./lib/index.d.ts",
"engines": {
"node": ">=12"
Expand All @@ -181,7 +190,6 @@
"cli-table3": "^0.6.0",
"colors": "^1.4.0",
"commander": "^8.0.0",
"create-require": "^1.1.1",
"duration": "^0.2.2",
"durations": "^3.4.2",
"figures": "^3.2.0",
Expand Down Expand Up @@ -255,7 +263,7 @@
"typescript": "4.3.5"
},
"scripts": {
"build-local": "tsc -p tsconfig.node.json",
"build-local": "tsc -p tsconfig.node.json && cp src/importers.js lib/ && cp src/wrapper.mjs lib/",
"cck-test": "mocha 'compatibility/**/*_spec.ts'",
"feature-test": "node ./bin/cucumber-js",
"html-formatter": "node ./bin/cucumber-js --profile htmlFormatter",
Expand Down
2 changes: 2 additions & 0 deletions src/cli/argv_parser.ts
Expand Up @@ -21,6 +21,7 @@ export interface IParsedArgvFormatOptions {
export interface IParsedArgvOptions {
backtrace: boolean
dryRun: boolean
esm: boolean
exit: boolean
failFast: boolean
format: string[]
Expand Down Expand Up @@ -111,6 +112,7 @@ const ArgvParser = {
'invoke formatters without executing steps',
false
)
.option('--esm', 'import support code via ES module imports', false)
.option(
'--exit',
'force shutdown of the event loop when the test run has finished: cucumber will call process.exit',
Expand Down
4 changes: 3 additions & 1 deletion src/cli/configuration_builder.ts
Expand Up @@ -18,6 +18,7 @@ export interface IConfigurationFormat {
}

export interface IConfiguration {
esm: boolean
featureDefaultLanguage: string
featurePaths: string[]
formats: IConfigurationFormat[]
Expand Down Expand Up @@ -77,10 +78,11 @@ export default class ConfigurationBuilder {
}
supportCodePaths = await this.expandPaths(
unexpandedSupportCodePaths,
'.js'
this.options.esm ? '.@(js|mjs)' : '.js'
)
}
return {
esm: this.options.esm,
featureDefaultLanguage: this.options.language,
featurePaths,
formats: this.getFormats(),
Expand Down