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

1

17

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"
    }
Valet answered 17/7, 2018 at 10:30 Comment(1)
This may not be possible: nyc runs mocha as a sub-process and Visual Studio Code is only attaching to the nyc process with no debug ability on the nyc sub-process. Personally I fell back to two separate runs: run nyc without debug for coverage reporting then separately run mocha for debugging. Good luck! P.S. Thanks for the outputCapture: std - without this the above has no output in VS Code for me.Lariat
K
6

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.

Krafftebing answered 19/5, 2020 at 19:35 Comment(1)
Just saw this, but I'm not doing anything with node.js right now, so I'll check it when I do, thanks! And can you please not address me as bro or man? Thanks.Valet

© 2022 - 2024 — McMap. All rights reserved.