Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug unit tests written in Typescript with Mocha from Visual Studio Code

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?

like image 285
Vereb Avatar asked Mar 25 '17 08:03

Vereb


People also ask

How do I debug mocha tests in Vscode?

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 .

How do you run a debug in mocha?

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.

How do I debug a unit test in Visual Studio?

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.


2 Answers

updating this thread to the config that worked for us (note to self).

  • the --compilers option in https://stackoverflow.com/a/44999572/147530 is deprecated
  • ts:ts-node/register gives error
  • adding "${relativeFile}" also gives error Unexpected token / in JSON at position 6 as the "${relativeFile}" resolves to .vscode/launch.json .

updated 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}"
}
like image 145
morpheus Avatar answered Oct 19 '22 04:10

morpheus


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.

like image 43
px1mp Avatar answered Oct 19 '22 05:10

px1mp