Node version: 16.13.2
Cucumber-js version: 7.3.2
Problem:
I cannot import into my step definitions, a helper function exported from another file.
I want to import the function, hex2rgb()
located in test/features/support/helpers.js
into one of my step definitions, located in test/features/step_definitions/steps.js. The function takes a string argument, converts it to an rgb value and returns it.
The project structure at /home/jimjamz/dev/colours:
├── package.json
└── test
├── features
│ ├── colours.feature
│ ├── step_definitions
│ │ └── steps.js
│ └── support
│ ├── helpers.js
│ └── support.js
Tests are run by executing ./node_modules/.bin/cucumber-js test
.
Question: What is the most appropriate way to export a helper function from another file, and import it into my cucumber-js step definitions?
There are a variety of ways I have configured the export and import of the helper function, all resulting in varying errors.
1) ECMAScript
helpers.js:
export function hex2rgb(in_hex) {
...
steps.js:
import { hex2rgb } from '../support/helpers.js';
Then('I should see the colour {string}', async function (colour) {
rgb_colour = hex2rgb(colour);
}
or steps.js:
const { hex2rgb } = require('../support/helpers');
Result:
SyntaxError: Cannot use import statement outside a module
SyntaxError: Unexpected token 'export' // this error can be resolved by renaming the helper file to helpers.mjs
2) CommonJS
helpers.js:
exports = function hex2rgb(in_hex)
steps.js:
let helpers = require('../support/helpers');
Then('I should see the colour {string}', async function (colour) {
rgb_colour = helpers.hex2rgb(colour);
}
Result:
TypeError: helpers.hex2rgb is not a function
3) NodeJS Modules
Added "type": "module",
to package.json at project root level (otherwise, ReferenceError: modules is not defined
).
helpers.js:
modules.export = function hex2rgb(in_hex)
steps.js:
const { hex2rgb } = require('../support/helpers');
Result:
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/jimjamz/dev/colours/test/features/step_definitions/steps.js from /home/jimjamz/dev/colours/node_modules/@cucumber/cucumber/lib/cli/index.js not supported.
Instead change the require of steps.js in /home/jimjamz/dev/colours/node_modules/@cucumber/cucumber/lib/cli/index.js to a dynamic import() which is available in all CommonJS modules.
at /home/jimjamz/dev/colours/node_modules/@cucumber/cucumber/lib/cli/index.js:122:17
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/jimjamz/dev/colours/test/features/support/helpers.js from /home/jimjamz/dev/colours/node_modules/@cucumber/cucumber/lib/cli/index.js not supported.
helpers.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename helpers.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/jimjamz/dev/colours/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at /home/jimjamz/dev/colours/node_modules/@cucumber/cucumber/lib/cli/index.js:122:17
4) Renaming the helpers file to .mjs or .cjs I've tried several variations of the above, but with a renamed helpers function file with the .mjs and .cjs file extensions.
These result in the same errors as above, [ERR_REQUIRE_ESM]: require() not supported. Instead change the require to a dynamic import()
, which when done, results in the SyntaxError: Cannot use import statement outside a module
error. Using ""type": "module
results in the same [ERR_REQUIRE_ESM]
as above. Switching to "type": "commonjs"
and using .cjs as the file extension, results in the TypeError: hex2rgb is not a function
when importing with require()
and SyntaxError: Cannot use import statement outside a module
when switching to use import
.
Resources I used:
Some of your examples are pretty close. I think this one matches your intent best, if you stick with CommonJS:
// helpers.js
function hex2rgb(in_hex) {...}
module.exports = {
hex2rgb
}
// steps.js
const { hex2rgb } = require('../support/helpers.js')