Mocha breakpoints using Visual Studio Code

后端 未结 20 1345
星月不相逢
星月不相逢 2020-12-12 11:52

Is it possible to add breakpoints to ones Mocha tests using Visual Studio Code?

Normally when debugging code one need to configure the launch.json, setting the prog

20条回答
  •  醉酒成梦
    2020-12-12 12:16

    When using TypeScript, the following configuration works for me in Visual Studio Code 0.8.0 (tsc 1.5.3)

    tsconfig.json

    {
        "compilerOptions": {
            "module": "commonjs",
            "target": "es5",
            "noImplicitAny": false,
            "removeComments": true,
            "preserveConstEnums": true,
            "sourceMap": true,
            "outDir": "build",
            "declaration": false
        },
        "files": [
            "./src/index.ts",
            "./src/test/appTests.ts"
        ]
    }
    

    The important things to note here is that source maps are generated and that the output directory for the js is set to build

    launch.json

        {
            "name": "Attach",
            "type": "node",
            // TCP/IP address. Default is "localhost".
            "address": "localhost",
            // Port to attach to.
            "port": 5858,
            "sourceMaps": true,
            "outDir": "build"
        }
    

    Please note that sourceMaps is set to true and that the outDir is set to build

    to debug

    1. Stick breakpoints in index.ts any other imported typescript file
    2. Open a terminal and run : mocha --debug-brk ./build/test/appTests.js
    3. From VSC, run the 'Attach' launch configuration

提交回复
热议问题