【问题标题】:Unable to compile Typescript using mocha and visual Studio Code Debugger无法使用 mocha 和 Visual Studio Code Debugger 编译 Typescript
【发布时间】:2020-12-22 05:02:56
【问题描述】:

我正在尝试使用 Visual Studio Code 和 mocha 调试单元测试,但是当 mocha 启动时,会引发此错误。

TSError: ⨯ 无法编译 TypeScript: mfa/test/index.test.ts(4,20): 错误 TS2307:找不到模块“断言”或其对应类型 声明。

我的launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "mocha tests",
      "type": "node",
      "protocol": "inspector",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "stopOnEntry": false,
      "args": [ "-r", "ts-node/register", "${workspaceRoot}/test/**/*.test.ts", "--no-timeouts"],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}"
      }
  ]
}

我的 tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "target": "ES2017", 
    "module": "commonJs",
    "allowJs": false, 
    "sourceMap": true, 

    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": false,

    "esModuleInterop": true, 



  
    /* Advanced Options */
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true, 
   
  }
}

【问题讨论】:

    标签: typescript visual-studio-code mocha.js


    【解决方案1】:

    Mocha 无法看到tsconfig.json。我已经解决了在TS_NODE_PROJECT 环境变量中设置tsconfig.json 路径。您需要将此 sn-p 添加到您的配置中:

    "env": {
        "TS_NODE_PROJECT": "${workspaceFolder}/tsconfig.json" // Or whatever path you have
    }
    

    【讨论】:

      【解决方案2】:

      您似乎忘记安装节点类型(其中包含断言类型),要修复此问题,请在项目文件夹中运行此命令。

      npm install --save @types/node
      

      【讨论】:

        猜你喜欢
        • 2017-08-02
        • 2018-03-12
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 2021-12-19
        • 2017-08-18
        相关资源
        最近更新 更多