英文:
Issues importing helper functions into cucumber-js step definitions
问题
Node版本: 16.13.2
Cucumber-js版本: 7.3.2
问题:
我无法从另一个文件中导入一个助手函数到我的步骤定义中。
我想要导入位于_test/features/support/helpers.js_中的函数hex2rgb()
,到我的步骤定义之一,位于_test/features/step_definitions/steps.js_中。 该函数接受一个字符串参数,将其转换为RGB值并返回。
项目结构在_home/jimjamz/dev/colours_中:
├── package.json
└── test
├── features
│ ├── colours.feature
│ ├── step_definitions
│ │ └── steps.js
│ └── support
│ ├── helpers.js
│ └── support.js
通过执行./node_modules/.bin/cucumber-js test
来运行测试。
问题: 从另一个文件中导出助手函数并将其导入到我的cucumber-js步骤定义中,最合适的方式是什么?
我已经配置了多种导出和导入助手函数的方式,但结果都导致不同的错误。
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);
}
或
steps.js:
const { hex2rgb } = require('../support/helpers');
结果:
SyntaxError: Cannot use import statement outside a module
SyntaxError: Unexpected token 'export' // 此错误可以通过将助手文件重命名为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);
}
结果:
TypeError: helpers.hex2rgb is not a function
3) NodeJS模块
在项目根目录的package.json中添加了"type": "module",
(否则,ReferenceError: modules is not defined
)。
helpers.js:
modules.export = function hex2rgb(in_hex)
steps.js:
const { hex2rgb } = require('../support/helpers');
结果:
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) 将助手文件重命名为.mjs或.cjs
我尝试了上述内容的几种变体,但使用带有.mjs和.cjs扩展名的重命名助手文件。
这导致与上述相同的错误,`[ERR_REQUIRE_ESM]: 不支持require()。改为使用动态import(),这会导致“SyntaxError: Cannot use import statement outside a module”错误。切换到“type”: “module”导致与上述相同的“[ERR_REQUIRE_ESM]”错误。切换到“type”: “commonjs”,并使用.cjs作为文件扩展名,在使用require()导入时导致“TypeError: hex2rgb is not a function”,切换到使用import时导致“SyntaxError: Cannot use import statement outside a module”错误。
我使用的资源:
-
https://medium.com/computed-comparisons/commonjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b
-
https://www.makeuseof.com/how-to-import-and-export-functions-in-javascript/
-
https://bobbyhadz.com/blog/javascript-syntaxerror-cannot-use-import-statement-outside-module
英文:
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:
答案1
得分: 1
以下是代码部分的翻译:
// helpers.js
function hex2rgb(in_hex) {...}
module.exports = {
hex2rgb
}
// steps.js
const { hex2rgb } = require('../support/helpers.js')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论