File tree Expand file tree Collapse file tree 5 files changed +53
-4
lines changed Expand file tree Collapse file tree 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/).
88Please see [ CONTRIBUTING.md] ( ./CONTRIBUTING.md ) on how to contribute to Cucumber.
99
1010## [ Unreleased]
11+
1112### Added
1213- 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 ) )
1315
1416### Changed
1517- 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
8282 When I run cucumber-js
8383 Then it runs 2 scenarios
8484 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'
22import { ISupportCodeLibrary } from '../support_code_library_builder/types'
33import supportCodeLibraryBuilder from '../support_code_library_builder'
44import { pathToFileURL } from 'url'
5+ import tryRequire from '../try_require'
56
67// eslint-disable-next-line @typescript-eslint/no-var-requires
78const { importer } = require ( '../importer' )
@@ -24,10 +25,13 @@ export async function getSupportCodeLibrary({
2425 requirePaths,
2526 importPaths,
2627 } )
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+
2932 for ( const path of importPaths ) {
3033 await importer ( pathToFileURL ( path ) )
3134 }
35+
3236 return supportCodeLibraryBuilder . finalize ( )
3337}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import {
1414 IWorkerCommandInitialize ,
1515 IWorkerCommandRun ,
1616} from './command_types'
17+ import tryRequire from '../../try_require'
1718
1819// eslint-disable-next-line @typescript-eslint/no-var-requires
1920const { importer } = require ( '../../importer' )
@@ -74,8 +75,8 @@ export default class Worker {
7475 requirePaths,
7576 importPaths,
7677 } )
77- requireModules . map ( ( module ) => require ( module ) )
78- requirePaths . map ( ( module ) => require ( module ) )
78+ requireModules . map ( ( module ) => tryRequire ( module ) )
79+ requirePaths . map ( ( module ) => tryRequire ( module ) )
7980 for ( const path of importPaths ) {
8081 await importer ( pathToFileURL ( path ) )
8182 }
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