简体   繁体   English

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

[英]How to run mocha in the same file with the JS script

Is it possible to have the JS code and the mocha tests in the same file?是否可以将 JS 代码和 mocha 测试放在同一个文件中? The purpose will be to have both the implementation and the tests in the same file when you just want to play with something, learn JS, prepare for an interview, etc.目的是当您只想玩一些东西、学习 JS、准备面试等时,将实现和测试都放在同一个文件中。

The file will be executed in VSCode using Debug (F5).该文件将使用 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();

Trying to run this example which is how you run mocha tests in browser, I get an error "mocha is not defined"尝试运行此示例,即在浏览器中运行 mocha 测试的方式,我收到错误消息“未定义 mocha”

I ran "npm install --save-dev mocha" and "npm install --save-dev chai".我运行了“npm install --save-dev mocha”和“npm install --save-dev chai”。 The file is test1.js.该文件是 test1.js。 In app.js I have "require("./test1")".在 app.js 我有“require("./test1")”。 The launch configuration is:启动配置为:

{
    // 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"
        }
    ]
}

After more googling I found the solution, your Debug launch.json file have to have the below configuration.经过更多的谷歌搜索,我找到了解决方案,您的 Debug launch.json 文件必须具有以下配置。 Basically the program you need to launch is "${workspaceFolder}/node_modules/mocha/bin/_mocha".基本上你需要启动的程序是“${workspaceFolder}/node_modules/mocha/bin/_mocha”。

You do not need any mocha commands in your JS file: mocha.setup("bdd");你的 JS 文件中不需要任何 mocha 命令: mocha.setup("bdd"); mocha.run();摩卡.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM