当 Mocha 和 IntellIJ IDEA 不雅 运行 时,如何为 TSNode 传递单独的 TypeScript 配置?

How to pass separate TypeScript config for TSNode when it has being indecency running by Mocha & IntellIJ IDEA?

虽然 TS-Node started the ECMAScript modules support,但它有很多限制,下面的例子:

import { assert as Assert } from "chai";


describe("firstTest", (): void => {

  it("", (): void => {
    Assert.isTrue(true);
  });

});

不适用于下一个设置:

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

extension:
  - ts

spec: "**/*.test.ts"

require:
  - ts-node/register
  - tsconfig-paths/register

错误:

import { assert as Assert } from "chai";
^^^^^^

SyntaxError: Cannot use import statement outside a module

我需要 ECMAScript 模块,因为 Webpack 动态加载 . So the conceptual solution is create the additional tsconfig.json for TSNode or pass modules type via console. It's has been documented 如何为 TSNode 执行此操作,但这里我不直接执行 TSNode - 我通过 IntelliJ IDEA 启动 Mocha:

IntelliJ IDEA,我想,包括 WebStorm 在内的该系列的所有 IDE 都会生成以下命令(添加了换行符):

"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-03.7148.57\plugins\NodeJS\js\mocha-intellij\lib\mochaIntellijReporter.js 
"D:\XXX\MyProject\Test\Test.test.ts" --grep "^firstTest "

现在如何设置CommonJS模块类型?

根据 ts-node 的文档 (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. 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. Note, multiple tsconfig.json files can use one common "base" file and extend/partially override it (see)。