Checking what methods were invoked during execution typescript on node js server (debugging nodejs) - node.js

I am developer with poor experience, however with strong academic CS background.
So please be patient where I am wrong somewhere.
As far I know, typescript can be executed on JS server and it works in this way in my case. Namely, I execute e2e tests with protactor (angular test framework).
I would like to debug it and check what methods was called during such execution on nodejs. To give you more details, I would like if (and possibly parameters) was invoked
waitForAngular(rootSelector, callback) method.
I know that it is possible to dump a variety of traces and debugging info from JVM, however I have no idea how can I do it with nodejs in my specific case.
Could you help me, please?

Around where you have your calls to waitForAngular (or any other code that you want to check), add a console.trace(), which will display a stack trace at that exact line of code.
You can read more about console.trace() and other console API methods at this document by the Chrome project (node.js uses the JS engine that Chrome uses): https://developers.google.com/web/tools/chrome-devtools/console/api#trace

You can sprinkle debugger statements in your code to stop execution which will allow you to step through your code. Similarly, you could start your Node process with the --inspect-brk argument to use Chrome DevTools to step through your code.

Related

How to debug a serverless framework plugin?

I've searched throughout google about this question and I had no success...
I want to work on a serverless plugin fix, but I don't know how to attach the process to debug the code.
Currently, I'm using vscode and the plugin was developed using nodejs + typescript.
Does anyone have any tip or article explaining how to do that?
As every other process, that you want to debug, you need to run it and somehow connect the debugger to it.
You need to remember, that Serverless Framework is written in JS/TS, so it runs in Node.js. So you can debug it quite easily, if you are developing your Lambdas in Node.js, as it's quite common environment.
How to do it using Jetbrains/Webstorm/IntelliJ
Go to your node_modules directory and find the source code of the plugin, that you want to debug.
In the source code place the breakpoint.
Now Create a new "Run configuration" in IDE for NPM, that should be similar to mine:
4. Make sure you've chosen the correct package.json!
5. Now simply start debugging, like you normally do, but choose the run configuration, that you've just created.
In my example I'm using package script from package.json, but it could be also any other script, that triggers serverless deploy or serverless print in the end.
And that's it! Breakpoints should be triggered normally, like when you debug your own JS code.

Detect if code using NodeJS specific global objects, what may not exist in browser

My code was written in in NodeJS, but It can also work in browser. But I made mistake earlier when I tried read process.versions variable directly.
Is there way write test in NodeJS and test am I using some node specific global objects or variables ?
Test has to fail since process variable is not defined in browser. But when I did search, Google gives me tutorials "How run my tests in browser", but I am not fan of that idea, I would love to stay in NodeJS. I think maybe some wrapper what hides those node specific global variables. Most likely someone has dome something like that, but google wasn't big help.
I am using mocha for testing, but I am open for suggestions.

How to debug a react.js library

I have been using react for about a year now and i recently wanted to delve into react-beautiful-dnd. I've built plugins for vanilla js and jQuery in the past , but i am not to sure about how to go about building a react.js plugin or even debug it. i am more interested in debugging this plugin and seeing how it works more then anything , so how exactly do i go about doing it ?
Typically with a JS plugin, its mostly one file , so a console.log inside each function would give you a clear enough understanding of why and when a certain function is triggered , how would you go about doing the same with a react.js plugin ?
There are multiple ways to debug.
node debug app.js
node-inspector
console
util
Well, I use a lot of well place console.log, console.dir and util.inspect statements throughout my code and follow the logic that way.
react-beautiful-dnd
Unfortunately there is not much documentation and do-how thing for given topic. But here is what I found helpful. you can follow this link for Quick start guide: https://github.com/atlassian/react-beautiful-dnd/issues/363
The codesandbox examples from the docs:
https://github.com/atlassian/react-beautiful-dnd#basic-usage-examples
Sample Project: https://github.com/jacobwicks/rbdnd-2-list-example
All Examples
Basic usage examples
We have created some basic examples on codesandbox for you to play with directly:
Simple vertical list
Simple horizontal list
Simple DnD between two lists
To Debug any Library:
Here's how you can debug that library. This is the most basic method.
Install it.
Check what you are using and what you need to debug.
Find that method in the node_modules.
Check if that method has any declaration in the code.
If yes put some console logs and see if they get printed on console.
Then check console for your logs.
Carry on the process of console logs until you get what you are looking for.
You have to find library's function you want to debug in node_module and console.log from there. You may need to console.log the parsed file usually found in node_module/plugin/lib or node_module/plugin/dist rather then the .jsx file in node_module/plugin/src.
How do I debug Node.js applications?.
This has quite a few answers on how you can go about debugging your react application.

NightmareJS without closing the browser

I'm used to PhantomJS and Watir, which both provide a REPL out of the box. The REPL lets me execute automation calls on a currently-running browser.
This is a fun way to make automation scripts because I can watch the effect of each step as I build an automation script.
In fact, I can even write a script that defines methods for opening a browser, performing a log-in, and other common tasks, and then call them as I please from within the generic Node or Ruby REPL.
Can I execute NightmareJS calls without closing the browser (Electron)?
Without closing? Yes - don't call .end().
If you're asking if you could disconnect the IPC - meaning the calling program ends but does not end the Nightmare instance - and then pick up the Nightmare instance again somewhere else, the answer is no.
#393 (packaging Nightmare functions with an existing Electron application) and #593 (covering v3 wants, including one Electron instance for many applications) are related, but I'm not convinced attaching IPC from new calling applications is a great idea.
If you provide more information about what your circumstances are and what you're trying to do, I'll edit this answer to try to help.
Having a REPL is a different question - I will add it to my list of things to look into. It's a good idea.

Node.js and Mocha Tests from the Browser

This is probably a really dumb question, but I'm not sure. If I have a node.js application and a much of tests built with Mocha, I can run them on my server no problems, using mocha. However, is there any way to invoke the unit tests through a browser and just pipe the results to the page? For development, I'd like to have a page on my website that shows the results of the tests.
Every time I search for how to do this, it's testing the actual browser code I'm finding (the dom, the UI etc), but it's actually just that I want to see the results of the tests run server-side but from the browser!
Thanks in advance!
Sure, create a route in Express that executes mocha and returns the result to the browser. Take a look at child_process.fork to execute mocha: http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
And don't forget to turn it off before going into production!
Separately, I would highly recommend to you the concept of continuous deployment, and specifically the service CircleCI, which for $19 automatically runs all of your tests every time you push to Github.

Resources