出于某种原因,我的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.1.1后发现了这个问题。
在launch.json上,将args数组从“tdd”更改为“bdd”,以便:
"-u", "bdd"
版本5.x使用“tdd”选项,因此下一个主要版本导致编写错误的配置打嗝。
也许它可能通过在mocha
中的types
中指定tsconfig.json
而起作用
{
"compilerOptions": {
...
"types": [
"node",
"mocha" <--- specify here
]
},
"compileOnSave": true
}
另外别忘了安装@types/mocha
npm install @types/mocha --save-dev
希望它可以解决您的问题
你的测试文件是用Javascript编写的(你在launch.json中引用* .js)?
我正在使用ts-node来调试单元测试,并直接引用Typescript测试文件,所以我的launch.json条目如下所示。在使用ts-node之前,我从VS Code内部运行时得到了'describe is not defined'错误。
{
"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"
}