[英]How to run mocha in the same file with the JS script
是否可以将 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();
尝试运行此示例,即在浏览器中运行 mocha 测试的方式,我收到错误消息“未定义 mocha”
我运行了“npm install --save-dev mocha”和“npm install --save-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}/node_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>/**"
],
},
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.