I'm using Mocha (and Chai) for my unit tests for a NodeJS module and want to debug it in Visual Studio code. I have a TypeScript file in the test
subfolder with some tests. VScode generates the .js and .map file in the out dir (via tsc watch mode task). My tsconfig.json file contains these settings:
{
"compilerOptions": {
"compileOnSave": true,
"module": "commonjs",
"target": "es6",
"outDir": "out",
"removeComments": true,
"noImplicitAny": true,
"sourceMap": true,
"inlineSources": true,
"isolatedModules": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true
},
"include": [
"src/**/*", "parser/**/*", "test/**/*"
],
"exclude": [
"node_modules",
".vscode-test"
]
}
and the out dir contains 3 subdirs for the 3 includes. All fine so far.
I can run my tests using this command:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
outside of vscode. Then I ran this code with the --debug-brk
switch and attached vscode to it. This works, but no breakpoint is hit. The configuration in launch.json for that is:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": true,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
Ideally, I'd like to have a run config so that I don't need to run mocha manually. With these settings I can at least run the tests:
{
"name": "Mocha",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"preLaunchTask": "tsc",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [ "--no-timeouts", "--colors", "${workspaceRoot}/out/test/**/*.js" ],
"stopOnEntry": true,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
}
"sourceMaps": true
}
but still, no breakpoint is hit.
What is required to make at least one of the 2 scenarios work?
Update: meanwhile I found by accident that breakpoints start working when you add a debugger;
command somewhere in the test code and set at least one fresh breakpoint after it stopped on debugger;
. After that all following breakpoints in this single file work as expected. Looks almost like a bug to me.