.settings not working 'mocha' in Microsoft Visual Studio Code on Windows

790 Views Asked by At

Is it possible to add breakpoints to one's Mocha tests using Visual Studio Code on Windows 7?

I tried to run a test with the following settings. (See here & here for reference.)

{
    "name": "Unit tests",
    "type": "node",
    "program": "node_modules/mocha/bin/_mocha",
    "stopOnEntry": false,
    "args": ["test/*.js"],
    "cwd": ".",
    "runtimeExecutable": "C:/Program Files/nodejs/node.exe",
    "env": { }
}
// Visual Studio Code 0.3.0
// Windows7(64bit)
// node v0.12.2
// [email protected]

But following this an error will be displayed:

can't launch program 'c:\Users\xxx\study_mocha\node_modules\mocha\bin_mocha'; enabling source maps might help

3

There are 3 best solutions below

1
On BEST ANSWER

I was able to get it to kind of work by hack alert copying _mocha to mymocha.js. Then Code recognizes it as something to run\start. It always runs the test but it doesn't always hit break points. I've found if I "stopOnEntry": true,on entry, then set breakpoints on the describe and it statements, I can hit on break points on the test.

The other tip that I have is that when the tests run, if they all pass the debugger window just closes and you don't get the satisfaction of seeing the passing tests output. So if, I'm really trying to debug, I put a assert.equal(1,2) so it fails and it will wait for a keypress (and I see the results page).

I know it's not a great answer. It's pretty much a work-around for beta software. 0.5.0

{
            "name": "Classification Tests",
            "type": "node",
            "program": "C:/c2/npm-global/node_modules/mocha/bin/myMocha.js",
            "stopOnEntry": true,
            "args": ["classificationTests.js"],
            "cwd": ".",
            "runtimeExecutable": null,
            "env": { }
        },  
1
On

I usually start mocha with

mocha --debug-brk

and then in Visual Studio Code debug with Attach. Works like a charm and without having to hard-code anything.

0
On

This solution worked with me

    {
        "name": "Mocha test",
        "type": "node",
        "request": "launch",
        "cwd": "${workspaceRoot}",
        "args": [
            "mochapack",
            "--timeout 20000",
            "--inspect-brk", 
            "--webpack-config=node_modules/@vue/cli-service/webpack.config.js",
            "--require tests/Vue/setup.js",
            "--require tests/Vue/mock-local-storage",
            "\"tests/**/*.spec.ts\""
        ],
        "windows": {
            "program": "${workspaceFolder}/node_modules/mochapack/bin/mochapack",
        },
        "internalConsoleOptions": "openOnSessionStart"
      }