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

I am writing a Typescript library. Unit tests are also written in Typescript using the Mocha framework. I would like to run unit tests directly without compilation in javascript. This works with this command:

./node_modules/mocha/bin/mocha  ./test/*.test.ts  --require ts-node/register

I am trying to debug unit test from Visual Studio code with the following startup 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"
}

That way I can debug Mocha itself from VS Code, but not unit tests. Mocha generates separate processes for tests, and the debugger cannot automatically join child processes.

What is the correct way to debug Typescript unit tests from Visual Studio code?

+6
2

tl; dr: Nodejs


/ , 100% .

--require ts-node/register Mocha, ts-node/register.js . , .ts, typescript .

, ts- node/register, Nodejs Mocha. debug_test.js debug_test.ts. debug_test.js debug_test.ts.

Node.js: Launch Program ' Vissual Studio Code.

enter image description here

debug_test.ts:2, - ​​ .

enter image description here

Nodejs debugger, , .

debugger, Visual Studio Code , , .

enter image description here

, VS , Mocha .

+1

- , launch.json - :

    {
    "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}"
    }

node v7.10.0, tsc 2.4.0 Visual Studio Code 1.13.1. mocha, tsc node_modules.

+3

Source: https://habr.com/ru/post/1016031/


All Articles