File tree 5 files changed +53
-4
lines changed
5 files changed +53
-4
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
8
8
Please see [ CONTRIBUTING.md] ( ./CONTRIBUTING.md ) on how to contribute to Cucumber.
9
9
10
10
## [ Unreleased]
11
+
11
12
### Added
12
13
- Formatters create sub-directory automatically instead of failing ([ #2266 ] ( https://github.com/cucumber/cucumber-js/pull/2266 ) )
14
+ - Include a less cryptic error message when trying to ` require ` an ES module ([ #2264 ] ( https://github.com/cucumber/cucumber-js/pull/2264 ) )
13
15
14
16
### Changed
15
17
- Change hashes type from ` any ` to ` Record<string, string> ` in ` DataTable ` ([ #2270 ] ( https://github.com/cucumber/cucumber-js/pull/2270 ) )
Original file line number Diff line number Diff line change @@ -82,3 +82,23 @@ Feature: ES modules support
82
82
When I run cucumber-js
83
83
Then it runs 2 scenarios
84
84
And it passes
85
+
86
+ Scenario : ES module invoked with --require
87
+ Given a file named "features/a.feature" with:
88
+ """
89
+ Feature:
90
+ Scenario: one
91
+ Given a step passes
92
+ """
93
+ And a file named "features/step_definitions/cucumber_steps.js" with:
94
+ """
95
+ import {Given} from '@cucumber/cucumber'
96
+
97
+ Given(/^a step passes$/, function() {});
98
+ """
99
+ When I run cucumber-js with `--require features/**/*.js`
100
+ Then it fails
101
+ And the error output contains the text:
102
+ """
103
+ Error: Cucumber expected a CommonJS module
104
+ """
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { IdGenerator } from '@cucumber/messages'
2
2
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
3
3
import supportCodeLibraryBuilder from '../support_code_library_builder'
4
4
import { pathToFileURL } from 'url'
5
+ import tryRequire from '../try_require'
5
6
6
7
// eslint-disable-next-line @typescript-eslint/no-var-requires
7
8
const { importer } = require ( '../importer' )
@@ -24,10 +25,13 @@ export async function getSupportCodeLibrary({
24
25
requirePaths,
25
26
importPaths,
26
27
} )
27
- requireModules . map ( ( module ) => require ( module ) )
28
- requirePaths . map ( ( path ) => require ( path ) )
28
+
29
+ requireModules . map ( ( module ) => tryRequire ( module ) )
30
+ requirePaths . map ( ( path ) => tryRequire ( path ) )
31
+
29
32
for ( const path of importPaths ) {
30
33
await importer ( pathToFileURL ( path ) )
31
34
}
35
+
32
36
return supportCodeLibraryBuilder . finalize ( )
33
37
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import {
14
14
IWorkerCommandInitialize ,
15
15
IWorkerCommandRun ,
16
16
} from './command_types'
17
+ import tryRequire from '../../try_require'
17
18
18
19
// eslint-disable-next-line @typescript-eslint/no-var-requires
19
20
const { importer } = require ( '../../importer' )
@@ -74,8 +75,8 @@ export default class Worker {
74
75
requirePaths,
75
76
importPaths,
76
77
} )
77
- requireModules . map ( ( module ) => require ( module ) )
78
- requirePaths . map ( ( module ) => require ( module ) )
78
+ requireModules . map ( ( module ) => tryRequire ( module ) )
79
+ requirePaths . map ( ( module ) => tryRequire ( module ) )
79
80
for ( const path of importPaths ) {
80
81
await importer ( pathToFileURL ( path ) )
81
82
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Provides a try guarded require call that will throw a more detailed error when
3
+ * the ERR_REQUIRE_ESM error code is encountered.
4
+ *
5
+ * @param {string } path File path to require from.
6
+ */
7
+ export default function tryRequire ( path : string ) {
8
+ try {
9
+ return require ( path )
10
+ } catch ( error ) {
11
+ if ( error . code === 'ERR_REQUIRE_ESM' ) {
12
+ throw Error (
13
+ `Cucumber expected a CommonJS module at '${ path } ' but found an ES module.
14
+ Either change the file to CommonJS syntax or use the --import directive instead of --require.
15
+
16
+ Original error message: ${ error . message } `
17
+ )
18
+ } else {
19
+ throw error
20
+ }
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments