Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug with VSCode when making a nyc coverage report?

I am trying to debug when running nyc instead of just while running the mocha tests, so I won't have to run tests twice each time.
VSCode runs the coverage and shows it to me, but it will not stop or verify breakpoints, how do I set it to properly debug?
Is it even possible?

My launch configuration:

{
        "type": "node",
        "request": "launch",
        "name": "Coverge",
        "program": "/usr/local/bin/nyc",
        "args": [
            "${workspaceFolder}/node_modules/mocha/bin/_mocha",
            "-u",
            "tdd",
            "--timeout",
            "999999",
            "--colors",
            "${workspaceFolder}/tests/*/*"
        ],
        "skipFiles": [
            "${workspaceFolder}/node_modules/**/*.js"
        ],
        "env": {},
        "outputCapture": "std",
        "internalConsoleOptions": "openOnSessionStart"
    }
like image 430
Didi Kohen Avatar asked Jul 17 '18 10:07

Didi Kohen


People also ask

How do I Debug using VSCode?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I get code coverage in VSCode?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.

How do you add a breakpoint in VS code?

Set breakpoints in source code To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint.


1 Answers

I got your back bro.

Since NYC runs the subprocess as a spawn it would not work. But you can run a what is called a Compound Launch, which in practice runs 2 processes and the first one connects to the second one that is there waiting, listening to a port (9229 by default) and then voila.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Coverage",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/node_modules/.bin/nyc",
            "args": [
                "-x","test","--reporter=lcov","--reporter=text",
                "node", "--inspect-brk",
                "./node_modules/.bin/mocha", "test", "--recursive", "--timeout=300000"
            ]
        }
        ,
        { // https://code.visualstudio.com/Docs/editor/debugging#_launch-versus-attach-configurations
            "type": "node",
            "name": "AttachMocha",
            "request": "attach",
            "port": 9229
        }
    ],
    // https://code.visualstudio.com/Docs/editor/debugging#_compound-launch-configurations
    "compounds": [
      {
        "name": "NYC/Mocha",
        "configurations": ["AttachMocha", "Coverage"]
      }
    ]
}

you are going to see NYC/Mocha on your debug run list.

like image 112
Mestre San Avatar answered Oct 17 '22 08:10

Mestre San