[英]How to pass separate TypeScript config for TSNode when it has being indecency running by Mocha & IntellIJ IDEA?
Although TS-Node started the ECMAScript modules support , it has a lot of limitations and below example:虽然 TS-Node开始支持 ECMAScript 模块,但它有很多限制,下面的例子:
import { assert as Assert } from "chai";
describe("firstTest", (): void => {
it("", (): void => {
Assert.isTrue(true);
});
});
does not work with next settings:不适用于下一个设置:
tsconfig.json tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"strict": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"baseUrl": "./",
"paths": {},
"noUnusedParameters": true,
"noImplicitReturns": true
}
}
.mocharc.yaml .mocharc.yaml
extension:
- ts
spec: "**/*.test.ts"
require:
- ts-node/register
- tsconfig-paths/register
Error:错误:
import { assert as Assert } from "chai";
^^^^^^
SyntaxError: Cannot use import statement outside a module
I need the ECMAScript modules because the Webpack dynamic loading does not work with CommonJS modules .我需要 ECMAScript 模块,因为 Webpack 动态加载不适用于 CommonJS 模块。 So the conceptual solution is create the additional
tsconfig.json
for TSNode or pass modules type via console.因此,概念解决方案是为 TSNode 创建额外的
tsconfig.json
或通过控制台传递模块类型。 It's has been documented how to do it for TSNode, but here I don't execute TSNode directly - I launch the Mocha via IntelliJ IDEA: 已经记录了如何为 TSNode 执行此操作,但在这里我不直接执行 TSNode - 我通过 IntelliJ IDEA 启动 Mocha:
IntelliJ IDEA and, I suppose, all IDEs of this family including WebStorm generates the below command (added the line breaks): IntelliJ IDEA 和我想这个系列的所有 IDE 包括 WebStorm 都会生成以下命令(添加了换行符):
"C:\Program Files\nodejs\node.exe"
"D:\XXX\MyProject\node_modules\mocha\bin\mocha" --require ts-node/register --ui bdd --reporter C:\Users\XXX\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\203.7148.57\plugins\NodeJS\js\mocha-intellij\lib\mochaIntellijReporter.js
"D:\XXX\MyProject\Test\Test.test.ts" --grep "^firstTest "
Now how to set CommonJS
modules type?现在如何设置
CommonJS
模块类型?
According to ts-node 's documentation ( search for IntelliJ ), you can create an alternative tsconfig.json (containing the option "module": "CommonJS" ) and enforce it for Mocha tests using environment variable TS_NODE_PROJECT .根据ts-node的文档(搜索 IntelliJ ),您可以创建一个替代 tsconfig.json (包含选项"module": "CommonJS" )并使用环境变量TS_NODE_PROJECT为 Mocha 测试强制执行它。 In the configuration corresponding to your Mocha testing, in the Environment variables section, you set this variable to the pathname of this alternative tsconfig.json.
在与您的 Mocha 测试对应的配置中,在Environment variables部分中,将此变量设置为此替代 tsconfig.json 的路径名。 Note, multiple tsconfig.json files can use one common "base" file and extend/partially override it ( see ).
请注意,多个 tsconfig.json 文件可以使用一个通用的“基本”文件并扩展/部分覆盖它(请参阅)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.