[英]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.