2

I am trying to debug mocha unit test in visual studio code. I followed this question and got this run configuration:

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

It works. But it does not stop at breakpoints! If I run the file with a normal launch configuration, breakpoints are not ignored.

Any Idea what could be the reason for this?

Community
  • 1
  • 1
Nathan
  • 7,099
  • 14
  • 61
  • 125

2 Answers2

3

This works for me, you need to point to _mocha. Using just mocha does not allow attaching breakpoints.

    {
        "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"
        }
    }
CS.
  • 766
  • 8
  • 24
  • Oh, I should have checked that myself. I though /usr/bin/mocha would be fine and did not think twice about it. – Nathan May 25 '16 at 12:27
0

If you include the port and the "--debug-brk" argument, you should be able to debug mocha unit tests. I have the following setup in my launch.json file. I included the "--recursive" argument as well so mocha would run all tests in subfolders as well. With this configuration file, I just set my VS Code debugger to use the "Debug Mocha Test" configuration and I'm able to hit breakpoints in any of my test files.

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

You can verify the port mocha will use for debugging by running mocha --debug-brk

P Walker
  • 532
  • 5
  • 15