Getting mocha debugging under VS Code to use the --file option of mocha to initiate each test

Viewed 770

My colleague who does not use VS Code had made a small typescript file to be run with every test. This could be run from the mocha command line by using the --file option. I attempted to put the --file option and args section in the .vscode/launch.json file but it seemed to get ignored.

After a lot of hair pulling I managed to get it working by using the --config option to point to a small .mocharc.js file that I put in .vscode folder to keep it out of the way. The .mocharc.js looked like this:

module.exports = {  
  file: ['./tests/testHelper.ts']
}

I put this here so either someone could post a better way, or someone might find it useful. The args section of launch.json looked like this:

"args": [
                "-r",
                "ts-node/register",
                "--config", "${workspaceRoot}/.vscode/.mocharc.js",
                "${relativeFile}",
                "--no-timeout"
            ],
1 Answers

I was able to make this work as initially described. Building off the Node.js: Launch Program launch configuration, adding program and args:

{
  "name": "Mocha: Current File",
  "type": "node",
  "request": "launch",
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
  "args": [
    "-r",
    "ts-node/register",
    "--file",
    "testHelper.ts",
    "${relativeFile}",
    "--no-timeout"
  ],
  "skipFiles": [
    "<node_internals>/**"
  ]
}

I am able to run the debugger against a test file, see that the test helper ran, and place breakpoints as necessary. You may verify against this minimal project setup: https://github.com/zbream/so-mocha-helper

So I suspect your issue may be something more specific to your project. However, this is difficult to tell without more information.

Related