asvetliakov/run-current-test

Debug current test

Clan-Utility opened this issue · 4 comments

Great plugin. Really saves time!

Any idea how to implement 'debug current test'?

Glad it helped you. Recent versions of vscode-jest extension have an ability to initiate debugging of single test case. It may work for you in the meantime.

Thanks. Will give it a try.

According to this blog, we could debug the current file, which saves a lot of times.

  1. create an empty file mocha-debug.opts
  2. add to launch.json
{
  "name": "Debug with mocha",
  "type": "node",
  "request": "launch",
  "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
  "stopOnEntry": false,
  "args": [
    "--no-timeouts", 
    "--opts", 
    "${workspaceRoot}/test/mocha-debug.opts", 
    "${relativeFile}"],
  "cwd": "${workspaceRoot}"
}

Another workaround.

[1] add --inspect-brk flag to runCurrentTest.run in VSCode's settings.json:

"runCurrentTest.run": "npm run test -- --inspect-brk -g \"^${fullTestName}$\""

The package.json for above npm run test has script:

  "scripts": {
    "test": "mocha test/js",
    ...

[2] Add an attach configuration to launch.json for remote debugging:

    {
      "type": "node",
      "request": "attach",
      "name": "ATTACH",
      "port": 9229
    }

[3] Put a break point in your test

[4] Run your test using this run-current-test extension, it will stop immediately in mocha.

[5] Launch the above attach configuration in VSCode. Notice your execution is stopped at start of mocha.

[6] Press F9 to continue execution to your breakpoint.

NOTE: You can get it working with --inspect instead of --inspect-brk--but there is a good chance the JVM will be past your breakpoint before the VSCode debugger attaches. --inspect makes for a nicer flow as you can optionally do [5] and skip [6].