Mocha breakpoints using Visual Studio Code

后端 未结 20 1282
星月不相逢
星月不相逢 2020-12-12 11:52

Is it possible to add breakpoints to ones Mocha tests using Visual Studio Code?

Normally when debugging code one need to configure the launch.json, setting the prog

相关标签:
20条回答
  • 2020-12-12 12:22

    If you have some dependency in the test it is also easy to attach it.

    For example, I am using mongo-unit-helper to also have unit tests integrated with Database.

    package.json script is: mocha --recursive --require ./test/mongo-unit-helper.js --exit"

    My launch.json looks like:

      "configurations": [
      {
      "type": "node",
      "request": "launch",
      "name": "Mocha Tests",
      "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
      "args": [
        "-u",
        "tdd",
        "--timeout",
        "999999",
        "--colors",
        "--recursive",
        "--require",
        "${workspaceFolder}/test/mongo-unit-helper.js",
        "${workspaceFolder}/test/**/*.js",
      ],
      "internalConsoleOptions": "openOnSessionStart"
     }
    ]
    

    Solution is to put --require separately in args in launch.json.

    0 讨论(0)
  • 2020-12-12 12:24

    The way I got it to work on VS Code (1.8.2) on Mac OS X is:

    {
        "name": "Mocha",
        "type": "node",
        "request": "launch",
        "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
        "stopOnEntry": false,
        "args": ["--recursive"], //you can specify paths to specific tests here
        "cwd": "${workspaceRoot}",
        "runtimeExecutable": null,
        "env": {
            "NODE_ENV": "testing"
        }
    }
    

    Mocha needs to be installed in the npm modules directory.

    0 讨论(0)
  • 2020-12-12 12:24

    1) Go to

    .vscode

    then

    launch.json

    file

    2) Add the following configuration in launch.json -

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Mocha Test",
                "cwd": "${workspaceRoot}",
                "runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha",
                "windows": {
                    "runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha.cmd"
                },
                "runtimeArgs": [
                    "--colors",
                    "--recursive",
                    "${workspaceRoot}/*folder_path_till_test*/tests"
                ],
                "internalConsoleOptions": "openOnSessionStart"
            },
            {
                "type": "node",
                "request": "launch",
                "name": "Launch Program",
                "program": "${workspaceRoot}/*folder_path_to_test*/app.js"
            }
        ]
    }
    

    3) Set breakpoints in test file and then press F5

    0 讨论(0)
  • 2020-12-12 12:25

    If you add ${file} variable at the end of the args list you can start debugging directly from the file you have open:

            {
                "type": "node",
                "request": "launch",
                "name": "Mocha Tests",
                "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
                "args": [
                    "-u",
                    "tdd",
                    "--timeout",
                    "999999",
                    "--colors",
                    "${file}"
                ],
                "internalConsoleOptions": "openOnSessionStart"
            }
    
    0 讨论(0)
  • 2020-12-12 12:25

    Here is an example of launch configuration (launch.json) from Microsoft, which works with Mocha and allows using the debugger.

    Also, there is a description of how to use the --debug-brk option.

    Finally, here is an alternative version of how to debug code with Mocha tests using tasks.json file of VS Code and Gulp task runner.

    0 讨论(0)
  • 2020-12-12 12:28

    Another way is to use the --debug-brk command line option of mocha and the default Attach launch setting of the Visual Studio Code debugger.


    Suggested deeper explanation (from André)

    To do this:

    Run mocha from the command line using this command:

    mocha --debug-brk
    

    Now in VS Code click on the Debug icon, then select Attach from the option next to the start button. Add breakpoints in VS Code and then click start.

    0 讨论(0)
提交回复
热议问题