Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging mocha tests with babel in Visual Studio Code

I am attempting to debug tests written in es6 in Visual Studio Code however the line numbering is all wrong: the break points work and I can step through the code but the highlighted line is on the wrong line.

The code I am seeing in Visual Studio Code is the es6 source code not the es5 babel is configured to output. The line numbering seems to line up with what I imagine the es5 code would look like.

Here is my Visual Studio Code configuration, note I have set sourceMaps to true and outDir to null as recommended in this question but it still does not work:

{
  "version": "0.1.0",
  // List of configurations. Add new configurations or edit existing ones.  
  // ONLY "node" and "mono" are supported, change "type" to switch.
  "configurations": [
    {
      // Name of configuration; appears in the launch configuration drop down menu.
      "name": "Debug mocha",
      // Type of configuration. Possible values: "node", "mono".
      "type": "node",
      // Workspace relative or absolute path to the program.
      "program": "${workspaceRoot}\\node_modules\\mocha\\bin\\_mocha",
      // Automatically stop program after launch.
      "stopOnEntry": false,
      // Command line arguments passed to the program.
      "args": [
                "test/.setup.js",
                "--reporter", "list",
                "--compilers", "js:babel/register",
                "--recursive", "./src/**/*.spec.js", "./src/**/*.integrationSpec.js", "./test/**/*.spec.js"
            ],
            // Ensure use sourcemaps generated by babel
            "sourceMaps": true,
      // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
      "cwd": "${workspaceRoot}",
      // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
      "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
      // Environment variables passed to the program.
      "env": {
                "NODE_PATH": "${workspaceRoot}\\src;${workspaceRoot}\\src\\framework\\core\\PageBuilder;${workspaceRoot}\\test\\testUtilities", 
                "NODE_ENV": "test"
            },
            "externalConsole": false,
            "outDir": null           

    }
  ]
}

I am using Visual Studio Code version 0.10.11. Node version 5.7.0 Mocha version 2.3.3

like image 289
Jack Allan Avatar asked Apr 08 '16 09:04

Jack Allan


People also ask

How do I Debug mocha tests in VS Code?

The simplest thing in the world. Vscode already comes with debug extension by default in latests versions, you just need to click in the "prevent bug" icon or a "bug with play" icon, after this click on cogwheel icon and select configure or fix launch. json .

How do you Debug test cases in VS Code?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.

How do you run a Debug in mocha?

run mocha with flag --inspect-brk and click 'open dedicated DevTools for node' in chrome from page chrome://inspect . In dedicated DevTools window add connection localhost:9229 to connect automatically. Also add a debugger statement to the file you want debug.

How do you run all tests in VS Code?

Tests can be run from Test Explorer by right-clicking in the code editor on a test and selecting Run test or by using the default Test Explorer shortcuts in Visual Studio. Some of the shortcuts are context-based. This means that they run or debug tests based on where your cursor is in the code editor.


1 Answers

The following "launch.json" is working for me with mocha and babel:

{
  "type": "node",
  "request": "launch",
  "name": "Debug Mocha",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/mocha",
  "runtimeArgs": [
    "--require",
    "babel-polyfill",
    "--require",
    "@babel/register",
    "--recursive",
    "${workspaceFolder}/tests"
  ],
  "internalConsoleOptions": "openOnSessionStart"
}

To fix problems with the breakpoints stopping at the wrong lines, open your ".babelrc" file and add "sourceMaps" and "retainLines", mine looks like:

{
  "presets": ["@babel/preset-env"],
  "sourceMaps": "inline",
  "retainLines": true,
}
like image 143
Vegard Avatar answered Oct 06 '22 01:10

Vegard