在Mocha中为ts-node覆盖`tsconfig.json` [英] Overriding `tsconfig.json` for ts-node in mocha
本文介绍了在Mocha中为ts-node覆盖`tsconfig.json`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以覆盖从mocha调用时ts节点使用的tsconfig.json
ts节点?
Is it possible to override which tsconfig.json
ts-node uses when called from mocha?
我的主要tsconfig.json
包含"module": "es2015"
,但是我只想将"module": "commonjs"
用于ts节点.
My main tsconfig.json
contains "module": "es2015"
, but I want to use "module": "commonjs"
for ts-node only.
我尝试过
mocha --compilers ts:ts-node/register,tsx:ts-node/register \
--compilerOptions '{"module":"commonjs"}' \
--require ts-node/register test/**/*.spec.ts*
但是它不起作用:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Module.m._compile (/usr/lib/node_modules/ts-node/src/index.ts:406:23)
at Module._extensions..js (module.js:422:10)
at Object.require.extensions.(anonymous function) [as .tsx] (/usr/lib/node_modules/ts-node/src/index.ts:409:12)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at /usr/lib/node_modules/mocha/lib/mocha.js:222:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/lib/node_modules/mocha/lib/mocha.js:219:14)
at Mocha.run (/usr/lib/node_modules/mocha/lib/mocha.js:487:10)
at Object.<anonymous> (/usr/lib/node_modules/mocha/bin/_mocha:458:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
推荐答案
您需要通过TS_NODE_COMPILER_OPTIONS
环境变量设置设置
You need to set the configuration through the TS_NODE_COMPILER_OPTIONS
environment variable
Unix机器上的示例代码:
Example code on an unix machine:
TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}' \
mocha --require ts-node/register 'test/**/*.spec.{ts,tsx}'
Explanation extracted from the repository documentation
CLI和程序化选项
用括号表示的环境变量.
-
-T, --transpile-only
使用TypeScript的更快的transpileModule(TS_NODE_TRANSPILE_ONLY
,默认值:false) -
-I, --ignore [pattern]
覆盖路径模式以跳过编译(TS_NODE_IGNORE
,默认值:/node_modules/) -
-P, --project [path]
TypeScript JSON项目文件(TS_NODE_PROJECT
)的路径 -
-C, --compiler [name]
指定自定义TypeScript编译器(TS_NODE_COMPILER
,默认值:打字稿) -
-D, --ignore-diagnostics [code]
通过诊断代码(TS_NODE_IGNORE_DIAGNOSTICS
)忽略TypeScript警告 -
-O, --compiler-options [opts]
JSON对象与编译器选项(TS_NODE_COMPILER_OPTIONS
)合并 -
--files
在启动时从tsconfig.json加载文件(TS_NODE_FILES
,默认值:false) -
--pretty
使用漂亮的诊断格式化程序(TS_NODE_PRETTY
,默认值:false) -
--skip-project
跳过项目配置解析和加载(TS_NODE_SKIP_PROJECT
,默认值:false) -
--skip-ignore
跳过忽略检查(TS_NODE_SKIP_IGNORE
,默认值:false) -
--log-error
记录类型错误而不是退出进程(TS_NODE_LOG_ERROR
,默认值:false) -
--prefer-ts-exts
重新排序文件扩展名,以便首选TypeScript导入(TS_NODE_PREFER_TS_EXTS
,默认值:false)
-T, --transpile-only
Use TypeScript's faster transpileModule (TS_NODE_TRANSPILE_ONLY
, default: false)-I, --ignore [pattern]
Override the path patterns to skip compilation (TS_NODE_IGNORE
, default: /node_modules/)-P, --project [path]
Path to TypeScript JSON project file (TS_NODE_PROJECT
)-C, --compiler [name]
Specify a custom TypeScript compiler (TS_NODE_COMPILER
, default: typescript)-D, --ignore-diagnostics [code]
Ignore TypeScript warnings by diagnostic code (TS_NODE_IGNORE_DIAGNOSTICS
)-O, --compiler-options [opts]
JSON object to merge with compiler options (TS_NODE_COMPILER_OPTIONS
)--files
Load files from tsconfig.json on startup (TS_NODE_FILES
, default: false)--pretty
Use pretty diagnostic formatter (TS_NODE_PRETTY
, default: false)--skip-project
Skip project config resolution and loading (TS_NODE_SKIP_PROJECT
, default: false)--skip-ignore
Skip ignore checks (TS_NODE_SKIP_IGNORE
, default: false)--log-error
Logs errors of types instead of exit the process (TS_NODE_LOG_ERROR
, default: false)--prefer-ts-exts
Re-order file extensions so that TypeScript imports are preferred (TS_NODE_PREFER_TS_EXTS
, default: false)
这篇关于在Mocha中为ts-node覆盖`tsconfig.json`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文