0

Does anyone know how to configure VSCode to debug Mocha tests when executing via a test script? Setup is:

  • "test" config in package.json of project specifying the mocha command to execute( mocha -R mochawesome -s 3000 -t 30000 ./index.js )
  • 'npm test' command used in internal terminal of VSCode with '-g' param to allow for execution of specific descriptions within CoffeeScript test files

I want to be able to debug the execution of these individual tests(i.e. run 'npm test -- -g "test description"' in VSCode and break in VSCode's Debug view when it reaches a bp). Is this possible? Would an 'attach' config be needed instead of 'launch'?

I've tried the standard debug configs provided in VSCode , and tried to modify them based on info found in various places, but no success so far. Any help would be great, not too familiar with the IDE, or any of these processes Thanks!

conorw89
  • 45
  • 1
  • 8

2 Answers2

1

Late answer but it may help people falling on this question.

Adding inspect-brk will hold the process until you connect your debugger, vscode in this case. After that tests will run and stop on your debug points. Usually the listening port for debugging is 9229, but you'll see the correct port on the sysout.

mocha --inspect-brk test.js

Credits to Run node inspector with mocha

0

You can attach vs code debugger to a process launched by script

For that you need to:

1) Add mocha's --inspect option to your script

2) Configure your launch.json this way

"configurations": [
    {
        "type": "node",
        "request": "attach",
        "name": "Mocha: tests",
        "processId": "${command:PickProcess}",
        "restart": true,
        "protocol": "inspector",
    },
]

3) After running your script hit F5 and pick mocha's process from vs code popped up processes list (you need to be quick here :) )

4) Second time you run the script and hit F5 vs code will automatically pick the right process for you

elch_yan
  • 61
  • 1
  • 10
  • Hi, thanks for the help! Unfortunately this doesn't seem to work for me, when i select the node process after pressing F5, nothing happens, and then I see an error 'cannot connect to runtime process, timeout after 1000ms - (reason: Cannot connect to the target: ECONNREFUSED 127.0.0.1:9229)'. Seems like there is some config issue with the port used for debugging node applications? – conorw89 Dec 05 '18 at 16:46
  • Can you provide your node and mocha versions? – elch_yan Dec 05 '18 at 18:25