Mocha breakpoints using Visual Studio Code

后端 未结 20 1280
星月不相逢
星月不相逢 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:01

    I've made this work on VSCode on OS X 10.10. Just replace your ./settings/launch.json file with this.

    {
        "version": "0.1.0",
        "configurations": [
            {
                "name": "Run app.js",
                "type": "node",
                "program": "app.js", // Assuming this is your main app file.
                "stopOnEntry": false,
                "args": [],
                "cwd": ".",
                "runtimeExecutable": null,
                "env": { "NODE_ENV": "production"}
            },
            {
                "name": "Run mocha",
                "type": "node",
                "program": "/Users/myname/myfolder/node_modules/mocha/bin/_mocha",
                "stopOnEntry": false,
                "args": ["test/unit.js"],
                "cwd": ".",
                "runtimeExecutable": null,
                "env": { "NODE_ENV": "production"}
            }
        ]
    }
    

    It is also available as a gist here.

    The key values you need to change are program, which should be set to the _mocha executable, and args, which should be an array of your test files.

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

    Sorry for adding yet another answer, but none of the previous ones quite worked for me as of VS Code 1.8.1 and the standard Node debugger included in it. Here is the way I solved it (with guidance from the previous answers here and from the official VS Code Node.js Debugging docs) so there is one click/keypress debugging:

    • Ensure mocha is installed as a devDependency in packages.json: "devDependencies": { "mocha": "^3.2", ... }
    • Run npm install in the directory of your package.json to make sure mocha is now installed in node_modules/
    • Open .vscode/launch.json (or in VS Code, press F1, start typing "launch", and select "Debug: Open launch.json")
    • Click the blue "Add Configuration" button in the bottom right (or just copy and paste one of your others); this step is optional... I mean, you can re-use an existing config. But I suggest adding one to keep it less confusing.
    • Change the following in your launch.json, then pick the new config name in the debug window in VS Code and click the green arrow to start debugging your node + mocha tests!

    In the new config in launch.json:

    "configurations": [{
        "name": "whatever name you want to show in the VS Code debug list",
        "type": "node",
        "cwd": "${workspaceRoot}",
        "program": "${workspaceRoot}/node_modules/mocha/bin/mocha",
        "args": ["--debug-brk=5858", "--no-timeouts", "--colors", "test/**/*.js"],
        "address": "localhost",
        "port": 5858,
        // the other default properties that are created for you are fine as-is
    }, ...]
    

    This assumes the pattern test/**/*.js will work for where you put your tests. Change as appropriate.

    Feel free to change the port as long as you change it in both of the args and port properties to match.

    The key differences for me was making sure mocha was in node_modules, using program to point to the executable, and args needing debug-brk=x pointing to the port specified in port. The rest of the above just makes things prettier and easier.

    It's up to you and your team if you put .vscode/launch.json in the repository or not. It's an IDE-only file, but your whole team could use it like this, no problem, since all paths and installs are relative and explicit.

    Tip: The package.json can include a scripts tag that also launches mocha with something like "test": "./node_modules/.bin/mocha", but it is not used by VS Code—instead it is used when npm test is run at the command line. This one confused me for a bit. Noting it here in case others get confused too.

    EDIT: VS Code 1.9.0 has added an "Add Configuration" option in the debug configuration drop-down, and you can pick "Node.js Mocha Tests" which help simplify most of the above. You still need to make sure mocha is in your node_modules and might have to update the cwd and last runtimeArgs (which is the pattern for finding your tests) to point to the appropriate paths. But once you set those two properties, it should work pretty much from there.

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

    If you don't want to use --debug-brk+Attach or state an absolute path to your global mocha installation (which will brake if you keep your launch.json under version control and have multiple developers on different machines), install mocha as a dev dependency and add this to your launch.json:

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

    Full debugging support in your tests by just pressing F5.

    --no-timeouts makes sure your tests don't time out because you stopped at a breakpoint, and --colors makes sure Mocha outputs colors even though it doesn't detect that VS Code supports colors.

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

    In VSCode version 1.13.0 (macOS), they have it built-in under configurations -> Mocha Tests.

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

    When using Babel, or generating javascript files yet placing breakpoints in the source - you have to make sure to enable sourceMaps and define outFiles. Here's an example config that worked for me.

        {
            "name": "Mocha Test",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/packages/api/node_modules/mocha/bin/_mocha",
            "cwd": "${workspaceRoot}/packages/api",
            "args": ["--colors", "--no-timeouts", "out/test"],
            "outFiles": ["${workspaceRoot}/packages/api/out/*"],
            "sourceMaps": true,
        },
    

    Note - you'll need to modify outFiles to include everything you might want to add a breakpoint to. This can be more tedious when in a monorepo and multiple dependent projects.

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

    For those that are using grunt or gulp, the configuration is pretty simple.

    Launch.json

    {
    "version": "0.2.0",
    "configurations": [
    
        {
            "name": "Run mocha by grunt",
            "type": "node",
            "program": "${workspaceRoot}/node_modules/grunt/bin/grunt",
            "stopOnEntry": false,
            "args": ["mochaTest"],
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": null
        }
    ]}
    

    Gruntfile.js

    module.exports = function (grunt) {
    
    grunt.initConfig({
        mochaTest: {
            test: {
                options: {
                    reporter: 'spec'
                },
                src: ['test/**/*test.js']
            }
        }
    });
    
    grunt.loadNpmTasks('grunt-mocha-test');
    
    grunt.registerTask('default', 'mochaTest');};
    
    0 讨论(0)
提交回复
热议问题