VSCode Debugging Mocha - Breakpoints Not Working? - node.js

I'm trying to debug my mocha tests with VSCode. The tests run to completion, but for some reason any breakpoints I set in my test file are greyed out and ignored with message, "Unverified breakpoints, breakpoints set but not yet bound." Here's my launch.json config:
{
"name": "Run mocha",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"--recursive",
"--compilers",
"js:babel-core/register",
"--require",
"babel-polyfill",
"--no-timeouts",
"--colors"
],
"runtimeExecutable": null,
"cwd": "${workspaceRoot}",
"env": { "NODE_ENV": "testing" },
"sourceMaps": false
}
I've gone through different Github issue threads and SO questions but have yet to get it to work. Any suggestions?

Related

Debugging Jest tests in VSCode does nothing

I'm trying to get debugging working with my NodeJS Jest tests in VSCode. I can run the tests without debugging fine, but trying extensions or debug options does nothing - meaning the tests don't run at all. I have a couple of breakpoints set - one is in a test, and the other is in the service file I'm trying to test.
Here's what I've tried:
My current launch.json file (I've tried other configurations that I've since removed):
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runTestsByPath",
"${relativeFile}",
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"name": "Jest", // This is the configuration name you will see in debug sidebar
"type": "node",
"request": "launch",
"port": 5858,
"address": "localhost",
"stopOnEntry": false,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "development"
},
"console": "integratedTerminal",
"runtimeArgs": [
"--inspect-brk", // node v8 use debug-brk if older version of node
"${workspaceRoot}/node_modules/jest/bin/jest",
"--watch",
"--bail",
"--runInBand"
],
"cwd": "${workspaceRoot}"
}
When clicking the green Play button (while the test file is open in the editor) for either of these under the "Run and Debug" tab, it looks like the debug options pop up for about a second, then they disappear and nothing happens.
In my package.json file, my test script looks as follows:
"test": "jest --watch --verbose"
and I've tried running npm test in a Javascript Debug Terminal. This runs the tests, but does not stop at the breakpoints I've set.
I've also tried a few extensions, such as "Jest", "Jest Runner" and "Jest Run It" - all having the same result of nothing happening when trying to run debug.
I'm running on a Windows 10 machine with my terminals in WSL Ubuntu.
Any help would be greatly appreciated. Thanks!
I would suggest you follow the following setup:
launch.json
{
"name": "Jest file",
"type": "pwa-node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest",
"args": [
"${fileBasenameNoExtension}",
"--runInBand",
"--watch",
"--coverage=false",
"--no-cache"
],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
},
Verify these libraries on package.json
"dependencies": {
"#types/jest": "^26.0.24",
"jest": "^27.0.6",
"ts-node-dev": "^1.1.8",
"typescript": "^4.3.5",
"ts-jest": "^27.0.3",
"ts-node": "^10.2.1"
}
The proposed configuration should let you debug your jest files

VS Code , Node Js Debugger Not working. Could Not find any debuggable target

I am trying to debug nodejs code using Launch.json attach to process. I have tried following configuration
but debugger is not getting connected. my application is running on Node v6.11.5
Launch.json :--
{
"type": "node",
"name": "Attach by Process ID",
"request": "attach",
"processId": "${command:PickProcess}",
"skipFiles": [
"<node_internals>/**"
],
}
,Error getting displayed:
If you are using gulp you can use a configuration like this to launch the gulp task from VSCode using the Run->Start Debugging F5 command and debug it directly.
{
"version": "0.2.0",
"configurations": [
{
"name": "MyGulpTask",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/gulp/bin/gulp.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"runtimeArgs": [
"--nolazy"
],
"console": "internalConsole",
}
]
}

How to debug while testing in sailsjs

I am trying to debug a 500 Internal server error in sailsjs. I have written tests in mocha and am getting the error from there. I am not sure on how to run a debugger when I start the testing with npm run test.
I am getting the error from another file, is it possible to put breakpoints over there and view it while testing
Thank you for your help
So, this is the solution that I came up with.
Using VScode,
configure the launch.json to be
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.spec.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Change according to how the test files are labeled in your project

debugging typescript in vscode with autorestart

I am setting up a typescript project with node.
I can debug my main.ts file in vs code with this launch.json configuration:
{
"type": "node",
"request": "launch",
"name": "Lancer le programme",
"program": "${workspaceRoot}/src/main.ts",
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
]
}
This works fine but there is no auto restart when I edit main.ts
To implement auto restart, I launch in my project directory tsc --watch, and then this lauch configuration:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--debug=5858"
],
"program": "${workspaceRoot}/src/main.ts",
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
],
"restart": true,
"port": 5858,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true
},
The above configuration does autorestart when I edit source files, but the vscode debugger doesn't break anymore...
Has anyone achieved : debugging typscript in vscode with autorestart ?
This is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/build/index.js",
"restart": true,
"runtimeArgs": [
"--debug=5858",
"--inspect-brk"
],
"port": 5858,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Note that the prop program must indicate compiled js file. In your case it is ${workspaceRoot}/src/main.ts, but should be ${workspaceRoot}/*compile directory eg. build*/main.js.
Also make sure the typescript recompiles the files to the target directory.

visual studio code debuggin mocha is ignoring breakpoints

I am trying to debug mocha unit test in visual studio code. I followed this question and got this run configuration:
{
"name": "Run mocha",
"type": "node",
"program": "/usr/bin/mocha",
"stopOnEntry": false,
"args": ["testUtils.js"],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"env": { "NODE_ENV": "development"}
},
It works. But it does not stop at breakpoints! If I run the file with a normal launch configuration, breakpoints are not ignored.
Any Idea what could be the reason for this?
This works for me, you need to point to _mocha. Using just mocha does not allow attaching breakpoints.
{
"name": "Debug mocha",
"type": "node",
"request": "launch",
"runtimeArgs": ["C:\\Users\\CS\\AppData\\Roaming\\npm\\node_modules\\mocha\\bin\\_mocha"],
"program": "${workspaceRoot}\\test.js",
"stopOnEntry": false,
"args": [
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"env": {
"NODE_ENV": "development"
}
}
If you include the port and the "--debug-brk" argument, you should be able to debug mocha unit tests. I have the following setup in my launch.json file. I included the "--recursive" argument as well so mocha would run all tests in subfolders as well. With this configuration file, I just set my VS Code debugger to use the "Debug Mocha Test" configuration and I'm able to hit breakpoints in any of my test files.
{
// Use IntelliSense to learn about possible Node.js debug 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": "Launch Program",
"program": "${workspaceRoot}/server.js",
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
},
{
"type": "node",
"request": "launch",
"name": "Debug Mocha Test",
"port": 5858,
"runtimeArgs": ["${workspaceRoot}/node_modules/mocha/bin/mocha"],
"cwd": "${workspaceRoot}",
"args": ["--recursive", "--debug-brk"]
}
]
}
You can verify the port mocha will use for debugging by running mocha --debug-brk

Resources