使用TypeScript进行路径映射不适用于黄瓜测试运行器 [英] Path mapping with typescript doesn't work with cucumber test runner
问题描述
在我的打字稿项目中运行Cucumber-js时,路径重新映射失败。使用相对路径很好,但是很难避免。例如。 import ... from @src /...\"
失败,而 import ... from ../../../ foo.ts 工作正常。
When running cucumber-js with my typescript project, path remapping fails. Using relative paths is fine but unforgivably ugly. E.g. import ... from "@src/..."
fails, while import ... from "../../../foo.ts"
works fine.
我的项目如下所示:
package.json
tsconfig.json
src/
**/*.ts
在 tsconfig.json
内,我指定重映射路径:
Inside tsconfig.json
, I specify remap paths:
"paths": {
"@src/*": [
"src/*"
],
"*": [
"node_modules/*",
"src/types/*"
]
我的命令行如下:
cucumber-js 'src/**/*.feature' --require-module ts-node/register --require 'src/**/*.steps.ts' --format-options '{"snippetInterface": "async-await"}' --format json:reports/cucumber-report.json --format summary --logLevel=error
以及堆栈跟踪:
Error: Cannot find module '@src/context/Bar'
at Function.Module._resolveFilename (module.js:513:15)
at Function.Module._load (module.js:463:25)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (.../foo.steps.ts:4:1)
at Module._compile (module.js:612:30)
at Module.m._compile (.../node_modules/ts-node/src/index.ts:414:23)
at Module._extensions..js (module.js:623:10)
at Object.require.extensions.(anonymous function) [as .ts] (.../node_modules/ts-node/src/index.ts:417:12)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at supportCodePaths.forEach.codePath (.../node_modules/cucumber/lib/cli/index.js:142:42)
在这种情况下,路径映射根本不起作用-特别是Cucumber-js。我究竟做错了什么?我改组了CLI参数,文件,但不走运。
The path mapping simply isn't working in this context – cucumber-js specifically. What am I doing wrong? I've shuffled the CLI args, files, no luck. Help... please?
推荐答案
该项目没有什么问题,但是因为它不知道路径映射机制,因为-需求模块ts-node / register
提供语言绑定,但不提供必要的路径解析智能。
There was nothing wrong with the project, but cucumber was unaware of the path mapping mechanism, as --require-module ts-node/register
provides language bindings, but not the necessary path resolution intelligence.
为此,您需要:-require-module tsconfig-paths / register
npm install --save-dev tsconfig-paths
注册模块
Register the module
./node_modules/.bin/cucumber-js ... \
--require-module ts-node/register \
--require-module tsconfig-paths/register \
...
这篇关于使用TypeScript进行路径映射不适用于黄瓜测试运行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!