visual studio code debuggin mocha忽略了断点

时间:2016-05-23 14:29:22

标签: node.js debugging visual-studio-code

我正在尝试在visual studio代码中调试mocha单元测试。我按照这个question进行了运行配置:

    {
        "name": "Run mocha",
        "type": "node",
        "program": "/usr/bin/mocha",
        "stopOnEntry": false,
        "args": ["testUtils.js"],
        "cwd": "${workspaceRoot}",
        "runtimeExecutable": null,
        "env": { "NODE_ENV": "development"}
    },

有效。但它并不止于断点!如果我使用正常启动配置运行文件,则不会忽略断点。

任何想法可能是什么原因?

2 个答案:

答案 0 :(得分:3)

这对我有用,你需要指向_mocha。仅使用mocha不允许附加断点。

    {
        "name": "Debug mocha",
        "type": "node",
        "request": "launch",
        "runtimeArgs": ["C:\\Users\\CS\\AppData\\Roaming\\npm\\node_modules\\mocha\\bin\\_mocha"],
        "program": "${workspaceRoot}\\test.js",
        "stopOnEntry": false,
        "args": [
        ],
        "cwd": "${workspaceRoot}",
        "runtimeExecutable": null,
        "env": {
            "NODE_ENV": "development"
        }
    }

答案 1 :(得分:0)

如果包含端口和“--debug-brk”参数,则应该能够调试mocha单元测试。我在launch.json文件中进行了以下设置。我还包含了“--recursive”参数,因此mocha也会在子文件夹中运行所有测试。使用此配置文件,我只需将我的VS Code调试器设置为使用“Debug Mocha Test”配置,我就可以在任何测试文件中点击断点。

{
    // Use IntelliSense to learn about possible Node.js debug attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/server.js",
            "cwd": "${workspaceRoot}"
        },
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Process",
            "port": 5858
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Mocha Test",
            "port": 5858,
            "runtimeArgs": ["${workspaceRoot}/node_modules/mocha/bin/mocha"],
            "cwd": "${workspaceRoot}",
            "args": ["--recursive", "--debug-brk"]
        }
    ]
}

您可以通过运行mocha --debug-brk

来验证mocha将用于调试的端口