【问题标题】:Describe is not defined exception on Typescript, Mocha and VSCode描述在 Typescript、Mocha 和 VSCode 上未定义异常
【发布时间】:2019-08-10 23:54:34
【问题描述】:

由于某种原因,我的 mocha 测试脚本抛出了“未定义描述”的异常。

我已阅读并尝试了这些 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。

【问题讨论】:

  • 找到了罪魁祸首——它是摩卡咖啡。从 6+ 降级到 5.2.0 版,它又可以工作了。然而,这只是一种解决方法,我需要找出真正的问题所在。

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


【解决方案1】:

也许可以通过在types 中指定mochatsconfig.json 中工作

{
  "compilerOptions": {
    ...
    "types": [
      "node",
      "mocha" <--- specify here
    ]
  },
  "compileOnSave": true
}

另外别忘了安装@types/mocha

npm install @types/mocha --save-dev

希望它能解决你的问题

【讨论】:

  • 在tsconfig.json和packages.json的types数组中添加了mocha,依然报错:TypeError: mocha_1.describe is not a function
  • 请看我对原帖的评论。
【解决方案2】:

您的测试文件是用 Javascript 编写的吗(您在 launch.json 中引用 *.js)?

我正在使用 ts-node 调试单元测试,并直接引用 Typescript 测试文件,所以我的 launch.json 条目如下所示。在我使用 ts-node 之前,从 VS Code 内部运行时出现“未定义描述”错误。

{
  "type": "node",
  "request": "launch",
  "name": "Unit tests (mocha)",
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
  "args": [
    "-r",
    "ts-node/register",
    "--timeout",
    "999999",
    "--colors",
    "${workspaceFolder}/test/**/*Test.ts",
  ],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "protocol": "inspector"
}

【讨论】:

  • 测试文件是用 Typescript 编写的。
  • 丹,请看我对上述问题的评论。
【解决方案3】:

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

我在安装 Mocha 6.1.1 后发现了这个问题。

在 launch.json 中,将 args 数组从“tdd”更改为“bdd”,这样:
"-u", "bdd"

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

【讨论】:

  • 不,你刚刚忘记了。
  • 实际上,在我们的例子中,样式是 bdd,这就是 tdd 选项不起作用的原因。
  • 感谢您回来回答您的问题。为我节省了大量时间。
【解决方案4】:

删除此导入:

import { describe, it } from 'mocha';

好像帮我解决了。

【讨论】:

  • 这似乎解决了我的问题。这有什么原因吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-18
  • 2019-08-12
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多