2

I have created a public repository to demonstrate this not working in a basic module that simply contains one mocha test file and a script to run said file through mocha with debugger attached:

https://github.com/corey-cosman/mocha-test-debug

to reproduce:

git clone git@github.com:corey-cosman/mocha-test-debug.git

npm install

npm run test:debug

expected:

that this test file runs and stops on breakpoint

actual:

the debugger attaches and listens on port 127.0.0.1:9229, but mocha file does not run

package.json:

{
  "name": "mocha-test-debug",
  "version": "1.0.0",
  "description": "",
  "main": "test/mocha-test-debug.js",
  "scripts": {
    "test:debug": "mocha -- --inspect-brk  ./test/mocha-test-debug.js"
  },
  "devDependencies": {
    "mocha": "^7.2.0"
  }
}

test/mocha-test-debug.js:

describe('mocha test debug', function () {
  it('should run test and hit breakpoint', async function () {
    debugger
  });
});

As mentioned, this has been working for a while and stopped very recently, noticed yesterday. Any help is greatly appreciated. Thanks!

coreycosman
  • 315
  • 2
  • 7

1 Answers1

1

That's happening because the --inspect-brk is breaking the script execution before it reaches your test script, which it's meant to do that. See https://nodejs.org/en/docs/guides/debugging-getting-started/

--inspect-brk Break before user code starts

You need open the devtools on chrome and then click on the green nodejs icon to see your applications debugger. And then click on resume execution (the blue play button) so that it moves to your debugger breakpoint

Also there is an error on your test:debug script, there is a typo on the file name.

This should be your test:debug command on package.json

"test:debug": "mocha -- --inspect-brk ./test/mocha-debug-test.js"

Zoilo Granda
  • 341
  • 2
  • 6
  • 1
    silly typo! (fixed file name on repo). Aside from typo, I forgot about that detail about the brk flag, that it breaks and requires a continue. The problem was that I have been debugging in VSCode lately and usually have Debug>Node: Auto Attachset to On for the debugger,which overrides that default inspect-brk behaviour and continues right to the breakpoint on script run,but somehow the setting got switched off without me realizing. Thanks for finding my typo and for helping me to remember this! If you use VSCode, and haven't tried what I have described, maybe you will enjoy! Thanks so much! :) – coreycosman May 28 '20 at 17:59