我正在尝试使用 launch.json 在 VS Code 中调试我的摩卡测试,但出现此错误:
ReferenceError: before is not defined
我还发现当我四处移动并注释掉代码部分时会出现这些错误:
ReferenceError: describe is not defined
ReferenceError: it is not defined
before、it和describe是摩卡函数。我查了一下摩卡正在推出,确实如此。 调试器工作正常,但一旦到达摩卡函数,就会抛出错误。
这是我的 launch.json:
{
"version": "0.2.0",
"configurations": [
{
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.test.js" // <-- minor change from default config
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js", // <-- mocha
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
这是 VS Code 自动生成的摩卡默认配置。
Mocha 有两个不同的接口:BDD 和 TDD。 BDD 是具有“describe”、“it”和“before”函数名称的接口。 TDD 功能相同,但名称不同。
您在代码中使用 BDD 接口,但在 launch.json 中告诉 VS Code 使用 TDD 接口启动 Mocha!
{
"version": "0.2.0",
"configurations": [
{
"args": [
"-u",
"bdd", // <- SOLUTION
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.test.js" // <-- minor change from default config
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js", // <-- mocha
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
https://mochajs.org/#command-line-usage
https://joshldavis.com/2013/05/27/difference- Between-tdd-and-bdd/