如何使用JS脚本在同一文件中运行mocha



是否可以将JS代码和mocha测试放在同一个文件中?目的是当你只想玩一些东西、学习JS、准备面试等时,将实现和测试都放在同一个文件中。

该文件将使用Debug(F5(在VSCode中执行。

function increment(n) {
return n + 1;
}
mocha.setup("bdd");
const { assert } = chai;
describe('Array', function () {
describe('#indexOf()', function () {
it('should increment', function () {
assert.equal(increment(1), 2);
});
});
});
mocha.run();

试图运行这个例子,也就是你如何在浏览器中运行摩卡测试,我得到了一个错误";摩卡没有被定义为";

我跑了";npm安装-保存dev-mocha";以及";npm安装—保存dev-chai";。文件是test1.js。在app.js中;require("./test1"(";。发射配置为:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
}
]
}

经过更多的谷歌搜索,我找到了解决方案,您的Debug launch.json文件必须具有以下配置。基本上,你需要启动的程序是"${workspaceFolder}/nod_modules/mocha/bin_mocha";。

在JS文件中不需要任何mocha命令:mocha.setup("bdd"(;摩卡.run((;

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Mocha Tests",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"bdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/thon-ly-40-questions"
],
"skipFiles": [
"<node_internals>/**"
],
},
]
}

最新更新