无法使用 mocha 和 visual Studio 代码调试器编译 Typescript

Unable to compile Typescript using mocha and visual Studio Code Debugger

我正在尝试使用 Visual Studio 代码和 mocha 调试单元测试,但是当 mocha 启动时,会抛出此错误。

TSError: ⨯ Unable to compile TypeScript: mfa/test/index.test.ts(4,20): error TS2307: Cannot find module 'assert' or its corresponding type declarations.

我的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, 
   
  }
}

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

npm install --save @types/node

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

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