how to debug nodeunit using node-inspector - node.js

I can do:
I can test node.js modules using nodeunit.
I can debug my node.js express site using node inspector.
But how to debug nodeunit test using node inspector?
I tried, but not working:
nodeunit --debug myNodeUnitModule_test.js It's not working.
I tried to install nodebug.
And used it like this: nodebug /usr/local/bin/nodeunit myNodeunit_test.js But it's not working neither on ubuntu (No such file or directory) nor on mac (env: node\r: No such file or directory)
Almost works
node --debug /usr/local/bin/nodeunit ./routes/edit/bodyTelInfoArraysToObject_test.js
where /usr/local/bin/nodeunit is path taken by command which nodeunit
got output:
debugger listening on port 5858
and test executed there.
But I can't jump in debuggin: when I open url localhost:8080 in chrome to watch debugging:
first load I see empty file list
second load: page not found.
On my nodeunit test I wrote debugger to stop on debug there.
But nothing.

In your tests insert debugger; command
exports['Main test'] = function(test){
debugger;
test.expect(1);
test.ok(true, 'Must be ok');
test.done();
};
And start all this
$ node --debug-brk `which nodeunit` test.js
Now in browser press F8, then F10, and you are right on the next line after first debugger; command in your test.
But I prefer to start everything with node-supervisor, that restart test automatically when test finished or files in project directory changed:
$ npm -g install supervisor node-inspector
$ # console 1
$ # supervisor restarts node-inspector when it quits
$ # ignores file changes
$ supervisor -i . -x node-inspector .
$ # console 2
$ supervisor --debug-brk -- `which nodeunit` test/index.js

Solution found:
in console:
node --debug-brk `which nodeunit` ./path/To/My/NodeUnitTests/nodeunit_test.coffee (Attention: `which nodeunit` is in back quotes)
in another console:
node-inspector &
And in google chrome open: http://0.0.0.0:8080/debug?port=5858
Here I see nodeunit debuging from the start. Click continue execution several times in browser until jump to nodeunit test, where I have debugger; string. So I debugging my nodeunit test with nodeinspector

Related

Nodejs waiting for 127.0.0.1:9229 to be free

When I try to use debugger in node to open debugger, I get an error 'Timeout (2000) waiting for 127.0.0.1:9229 to be free'. How can I resolve this and run the debugger correctly ?
function foo() {
var a = 5;
debugger
console.log(a)
}
foo()
I have already tried changing the port using node inspect --port=9230 app.js and it doesn't work.
Try this:
node --inspect-brk app.js
replace app.js with your file name that you want to run, and you can insert your additional command alongside with this line.
I had the same problem, it took me hours to figure it out...
Run the following command:
node inspect --port=9228 file.js
I had the same issue by using VS Code. VS code document helps. Replace program.js with your js file name.
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
if the program should not start running but must wait for the debugger to attach:
node --inspect-brk program.js
Use node --inspect-brk {js file name} instead. It will work.
--inspect-brk=[host:port]
Enable inspector agent
2.Bind to address or hostname host (default:127.0.0.1)
3.Listen on port port (default: 9229)
4.Break before user code starts
I think the issue here is the command you give to node, it should be node --inspect..
You are missing the -- in front of inspect :)
Install
npm install --global node-inspect
Then
node-inspect script.js
Ref: https://www.npmjs.com/package/node-inspect

docker node app always crashes on file change using nodemon

I am using the latest version of docker and the latest node image. I have a gulpfile that starts a nodemon process. I am using the --inspect flag to indicate I want to use the experimental chrome dev tool debugger. But when I make a file change it nodemon picks it up and restarts the process but crashes.
Here is my gulp task:
gulp.task('start:dev', done => {
let started = false;
nodemon({
script: path.join(__dirname, 'index.js'),
ext: 'js json',
nodeArgs: ['--inspect=0.0.0.0:9229'],
watch: path.join(__dirname, 'express'),
legacyWatch: true
})
.on('start', () => {
// to avoid nodemon being started multiple times
if (!started) {
setTimeout(() => done(), 100);
started = true;
}
});
});
And here is the error:
Starting inspector on 0.0.0.0:9229 failed: address already in use
If I change the --inspect flag to be --debug it works like a charm.
I am guessing is that the restart process is too fast for the --inspect to release its port. If I make another file change it does work and restarts normally. Probably since it had time to release the port.
I have tried using a delay on nodemon but I'd rather not. I would like quick restarts. And I have tried using to events, like, restart and exit, to wait for a few seconds and then restart the whole gulp task. But that was temperamental and again I want quick restarts without having to hack together something.
Right now I just switched back to --debug but that is deprecated in the latest V8. They are recommending to use --inspect.
Maybe the only way is to lock down my version of node?
Any suggestions?
There is an open issue addressing this problem.
The easiest workaround I found so far was using "signal": "SIGINT" in my nodemon.json thanks to this comment.
Just kill inspector and start inspector again
here is our team's solution in our package.json.
You had better kill inspector process and then restart inspector
"inspect": "kill-port --port 9229 && node --inspect=0.0.0.0:9229 build/startup.js",
"start_watch_inspect": `nodemon --delay 80ms --watch build/ build/startup.js --exec 'npm run inspect'`
Seems like this is related to:
https://github.com/remy/nodemon/issues/1492
My workaround is to run this before each restart: (in a makefile, gulp file etc...)
lsof -i -n | grep 9229 | awk '{print $2}' | xargs kill
** If put inside a Makefile remember to replace $ with $$ **

Node Inspector not hitting breakpoint for Intern test

I am trying to debug my intern test using node inspector. I followed all the steps correctly from here. I gave it a run by performing the following command
Start Selenium[for intern test]
Run the intern test in node environment
C:\node\node --debug-brk node_modules/intern/runner.js config=tests/intern
In another cmd I run the node inspector
node-inspector --web-port=9999 &
Start the browser
http://127.0.0.1:9999/?ws=127.0.0.1:9999&port=5858
I can see all the script of my project. I have break point set in one of the test component. But the moment I resume the debugger the test start running but the breakpoint is never hit. What am I doing wrong ?
I think you should add debugger; into your script as a break point

How to debug custom node server with brunch.io?

I am using brunch.io with a custom server app.coffee.
This is what my brunch-config.coffee entry looks like:
server:
path: 'app.coffee'
port: 3333
base: '/'
I want to use node's default debugger, but when I type debugger anywhere in my app.coffee, script execution doesn't stop. My debugger statement is simply ignored.
How can I make brunch run my server so that debugger statements are not ignored but pause script execution?
Brunch version: 1.7.18
Coffee version: 1.7.1
Node version: 0.10.30
Thanks for your time!
To use the debugger, it would be best to run brunch watch and your node app in separate terminal instances.
Just run brunch watch without the -s/--server option, and then separately run something like:
coffee --nodejs --debug app.coffee

Node-inspector not init breakpoints

I run node server in console
Then in browser :8080/debug?port=5858
i see
My breakpoint doesnt work .
Help me please.
You need to run node-inspector after your app.
Also, in your case, it seems by the time you start inspector, the app will be past the section you want to debug. So try this:
# Console window 1
node --debug-brk run.js
# Console window 2
node-inspector
Then navigate to http://localhost:8080/debug?port=5858
The execution should be stopped on the first line of code. Set up your breakpoints and resume.

Resources