How to debug while testing in sailsjs - node.js

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

Related

In VS Code why does Yarn debug stop at cli.js when Caught Exceptions is enabled?

I'm trying to debug a Jest test and breakpoint at "Caught Exceptions" in VS Code but it stops at /.node/corepack/yarn/1.22.15/lib/cli.js which is irrelevant to me.
I've tried to add several paths to skipFiles in launch.json but no luck so far.
How can I ignore the Yarn/node core files?
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests.v2",
"request": "launch",
"runtimeExecutable": "yarn",
"runtimeVersion": "16.13.2",
"program": "jest",
"args": [
"--runInBand",
"--watchAll=false",
"--testNamePattern",
"${jest.testNamePattern}",
"--runTestsByPath",
"${jest.testFile}"
],
"cwd": "${workspaceFolder}/next/",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "jest"
},
"skipFiles": [
"<node_internals>/**/*.js",
"~/.node/**/*.js",
"**/.yarn/**",
"**/node_modules/**",
"**/yarn/**",
]
},
]
}
Adding **/.node/corepack/yarn/** made it ignore those files.
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**",
"**/.node/corepack/yarn/**"
]

vscode can't debug mocha test with es6 babel

Getting error Cannot use import statement outside a module when runing from vscode debug.
When I run mocha from command line it works perfectly fine.
VSCode config:
{
"args": [
"test",
"--require #babel/register"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
command line which isworking: ./node_modules/mocha/bin/mocha test --require #babel/register --exit
This configuration worked for me
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/node_modules/mocha/bin/mocha",
"args": [
"test",
"--require", "#babel/register",
"--timeout", "999999",
"--colors"
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**"],
"sourceMaps": true,
"runtimeArgs": ["--nolazy"]
}
Also updated all bable packages to 7.x earlier I had babel-cli in my dependencies changed it with #babel/cli

Not connected to runtime

I am trying to debug my nodejs code in debug mode, but I am getting the following error in the debug console. Any idea?
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug content-service",
"program": "${workspaceFolder}\\src\\app.js",
"cwd": "${workspaceFolder}\\src",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Debug test",
"program": "${workspaceFolder}\\src\\node_modules\\lab\\bin\\lab",
"args": [
"--colors",
"${workspaceFolder}\\src\\test\\test.js"
],
"cwd": "${workspaceFolder}\\src",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
it works if I am not in debug mode.

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

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.

Resources