I write a Typescript library. The unit tests are also written in Typescript using Mocha framework. I'd like to execute the unit tests directly without compiling into javascript. This works with this command:
./node_modules/mocha/bin/mocha ./test/*.test.ts --require ts-node/register
I try to debug the unit test from Visual Studio Code with the following launch settings:
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--require",
"ts-node/register",
"${workspaceRoot}/test/*.test.ts"
],
"internalConsoleOptions": "openOnSessionStart"
}
This way I can debug Mocha itself from VS Code, but not the unit tests. Mocha spawns separate processes for the tests and the debugger can not automatically attach to the child processes.
What is the right way to debug Typescript unit tests from Visual Studio 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 .
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.
To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.
updating this thread to the config that worked for us (note to self).
--compilers
option in https://stackoverflow.com/a/44999572/147530 is deprecatedts:ts-node/register
gives errorupdated config
{
"name": "mocha tests",
"type": "node",
"protocol": "inspector",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [ "-r", "ts-node/register", "${workspaceRoot}/test/**/*.spec.ts", "--no-timeouts"],
"cwd": "${workspaceRoot}"
}
If anybody finds it useful, the following launch.json
configuration snippet is working for me without any workaround:
{
"name": "mocha tests",
"type": "node",
"protocol" : "inspector",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [ "--compilers", "ts:ts-node/register", "--no-timeouts", "${relativeFile}"],
"cwd": "${workspaceRoot}"
}
Works fine for me with node v7.10.0
, typescript 2.4.0
and Visual Studio Code 1.13.1 . Both mocha
and typescript
are installed locally under node_modules
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With