简体   繁体   English

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

[英]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.

相关问题 TSNode + Mocha:忽略 TypeScript 错误(使编译器发出错误并使 TSNode 执行发出的代码) - TSNode + Mocha: Ignore TypeScript errors (make compiler emit on errors and make TSNode execute emitted code) 如何在Intellij IDEA中调试打字稿 - How to debug typescript in Intellij IDEA 如何将Babel7配置文件路径传递给mocha和gulp-mocha? - How to pass Babel7 config file path to mocha and gulp-mocha? 如何在IntelliJ IDEA 13(或WebStorm)上远程运行mocha测试? - How can I run mocha tests remotely on IntelliJ IDEA 13 (or WebStorm)? PM2和tsnode:通过“ --fast”选项 - PM2 and tsnode : pass “--fast” option 运行mocha时找不到配置模块,但运行nodemon时可以工作 - Cannot find config module when running mocha but works when running nodemon 使用模块 es6 运行 TypeScript Mocha - Running TypeScript Mocha with module es6 Mocha:以编程方式运行 mocha 时获取堆栈跟踪 - Mocha: get stacktrace when running mocha programmatically 运行mocha时如何将特定的测试文件设置为第一个? - how to set a particular test file as the first when running mocha? 在运行摩卡单元测试时,如何避免在NodeJS回调样式API中两次调用Q promise catch块? - How can I avoid my Q promise catch block being called twice in a NodeJS callback-style API when running mocha unit tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM