在Cucumber-js步骤定义中导入辅助函数的方案咨询
- 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() not supported. Instead change the require to a dynamic import(),使用动态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错误。
内容的提问来源于stack exchange,提问作者jimjamz

