vscode-recipes/debugging-mocha-tests
stevengum b1376502ff add skipFiles with node_internals to debugging-mocha-tests 2020-03-11 10:10:16 -07:00
..
.vscode Add mocha test configuration that takes in option file 2018-10-13 21:01:14 +02:00
lib Added recipe for debugging Jest and Mocha tests in VS Code 2018-01-02 21:34:57 +00:00
support Add mocha test options file 2018-10-13 21:02:55 +02:00
test Spelling errors fixed 2018-10-30 20:16:26 -06:00
README.md add skipFiles with node_internals to debugging-mocha-tests 2020-03-11 10:10:16 -07:00
all.gif Added recipe for debugging Jest and Mocha tests in VS Code 2018-01-02 21:34:57 +00:00
current.gif Added recipe for debugging Jest and Mocha tests in VS Code 2018-01-02 21:34:57 +00:00
package.json Added recipe for debugging Jest and Mocha tests in VS Code 2018-01-02 21:34:57 +00:00

README.md

Debugging tests in VS Code

by Jag Reehal

This recipe shows how to use the built-in Node Debugger to debug Mocha tests.

The example

The test folder contains two files that test the lib/calc.js file.

To try the example you'll need to install dependencies by running:

npm install

Configure launch.json File for your test framework

  • Click on the Debugging icon in the Activity Bar to bring up the Debug view. Then click on the gear icon to configure a launch.json file, selecting Node for the environment:

  • Replace content of the generated launch.json with the following configurations:

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Mocha All",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "--timeout",
            "999999",
            "--colors",
            "${workspaceFolder}/test"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "skipFiles": [
            "<node_internals>/**/*.js"
        ]
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Mocha Current File",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "--timeout",
            "999999",
            "--colors",
            "${file}"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "skipFiles": [
            "<node_internals>/**/*.js"
        ]
    }
  ]
}

If you don't have all of your tests under a common "test" directory, then the following configurations can be used. It will recursively search for all *.test.js files except for those that are in a node_modules directory.

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Mocha All",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "--timeout",
            "999999",
            "--colors",
            "'${workspaceFolder}/{,!(node_modules)/}*/*.test.js'"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "skipFiles": [
            "<node_internals>/**/*.js"
        ]
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Mocha Current File",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "--timeout",
            "999999",
            "--colors",
            "${file}"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "skipFiles": [
            "<node_internals>/**/*.js"
        ]
    }
  ]
}

If you are running mocha will multiple arguments, you may consider creating an opt file that store all these arguments (i.e name it as mocha.opts).

Example file contents with mocha arguments:

--timeout 999999
--colors
--full-trace  

Reference the mocha opts file with --opts in configuration as shown below

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Mocha Test All with Options",
            "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
            "args": [
                "--opts", 
                "${workspaceFolder}/support/mocha.opts",
                "${workspaceFolder}/test"
            ],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "skipFiles": [
                "<node_internals>/**/*.js"
            ]
        }
    ]
}

Debugging all tests

You can debug all tests by following the steps below:

  1. Set a breakpoint in a test file or files

  2. Go to the Debug view, select the 'Mocha All' configuration, then press F5 or click the green play button.

  3. Your breakpoint will now be hit

all

Debugging the current test

You can debug the test you're editing by following the steps below:

  1. Set a breakpoint in a test file

  2. Go to the Debug view, select the 'Mocha Current File' configuration, then press F5 or click the green play button.

  3. Your breakpoint will now be hit

current