typescript - Describe is not defined exception on Typescript, Mocha 和 VSCode

标签 typescript visual-studio-code mocha.js

出于某种原因,我的 mocha 测试脚本抛出了“describe is not defined”的异常。

我已经阅读并尝试了这些 SO 问题建议的解决方案,但没有成功:
describe is not a function
"Mocha describe is not defined duplicate"

其他链接是:
typescript mocha describe is not a function

这是我的 VSCode launch.json。

{
  "type": "node",
  "request": "launch",
  "name": "Mocha Tests",
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
  "args": [
    "-u",
    "tdd",
    "--timeout",
    "999999",
    "--colors",
    "${workspaceRoot}/dist/tests/**/*.js"
  ],
  "outFiles": ["${workspaceFolder}/dist/tests/**/*.js"],
  "sourceMaps": true,
  "protocol": "inspector",
  "internalConsoleOptions": "openOnSessionStart"
}

这是我的 mocha 测试脚本:

import "mocha";
import assert = require("assert");

describe("Init", () => {
  before(() => {
    console.log("before-hook");
  });

  it("connected", () => {
    assert(true, "is not true");
  });
});

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "strict": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es6",
    "lib": [ "es6" ],
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "strictNullChecks": true,
    "allowJs": false,
    "checkJs": false,
    "types": [
      "node"
    ]
  },
  "compileOnSave": true
}

我在这里做错了什么?我真的需要重新开始使用 mocha。

最佳答案

在这里回答我自己的问题。

我在安装 Mocha 6.1.1 后解决了这个问题。

在 launch.json 上,将 args 数组从“tdd”更改为“bdd”,以便:
"-u", "bdd"

版本 5.x 使用“tdd”选项,因此下一个主要版本导致了配置编写错误的问题。

关于typescript - Describe is not defined exception on Typescript, Mocha 和 VSCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258209/

相关文章:

typescript - 如何在 vuex 存储 typescript 中动态设置对象的属性(同时保持类型安全)

visual-studio-code - 过滤 TreeView

node.js - 如何测试使用 JWT 身份验证的 Node API(使用用户登录获取 token )

node.js - 使用 Mocha 和 Should.js 组织测试

angular - 如何将 http 请求响应映射到我在 TypeScript 中定义的对象

typescript - angular 2自定义指令OnInit

javascript - nedb post数据仅在重启后出现

visual-studio-code - 如何在 Visual Studio Code 中使用全局 gitignore?

visual-studio-code - Visual Studio Code 突出显示标签

javascript - 在 Mocha 上循环()