Is there a way to slow down jest? - jestjs

I have some tests that pass on my local machine but fail intermittently in CI. I assume it is because the tests run slower in CI.
Is there a way to slowdown the tests on my local machine so that they fail consistently?

There is a way based on process priority. Here is a solution for Unix-like systems (for windows you have to find the relevant commands).
First, launch jest in debug mode with a breakpoint at the start, and connect your debugger (chrome devtools in general):
node --inspect-brk node_modules/.bin/jest --runInBand
Then find your process id:
ps -ax | grep node
Then lower the priority of the process:
renice -n 20 <pid>
Resume the jest tests in your debugger.
This should slow down the tests. If it is not enough, I suppose you can try to run some other process on your machine that should take priority.

Related

WebStorm Node.js debug with Docker

I'm trying to debug a Node.js script with WebStorm 2019.3 and Docker as a remote Node interpreter. So far I can start the script, debug it, but any changes done on local do not trigger a nodemon restart of the script inside the Docker container (files inside the container ARE actually changing, I've checked).
Any ideas? I'll attach the WebStorm run config.
I think there is something wrong about the way that I'm using nodemon when starting the script, but I have no idea how to fix it for WebStorm config.
Looks like you might need to enable legacyWatch.
According to the documentation:
In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enables Chokidar's polling.
Via the CLI, use either --legacy-watch or -L for short: nodemon -L

NodeJS: How to read stdout and stderr of a child process spawned by an explicitly spawned process?

I've recently been working on a on-demand build server. The build server is a NodeJS/Express REST API, which essentially wraps the Angular CLI and associated build tooling for completing a custom on-demand Angular application.
Everything is working as expected end to end, but I'd like to be able to get more granular with the status reporting, as Angular builds can be quite time consuming when factoring in 2 very large parts of the process
The two longest running parts of the process:
the npm install (generally automatically kicked off via the ng-new schematic from the default #angular/schematics collection)
the actual ng build command.
2 is easy to address, as I am spawning that process (ng build --prod) via child_process.spawn() directly.
1 has proven a bit more complicated, as the long running npm install process is actually kicked off internally to the default Angular ng-new schematic/command. So, if my thinking is correct, this is essentially a explicitly spawned child process (my spawned ng new) which is internally spawning npm install.
One work around that I've come up with is to pass in the --skip-install arg to ng new, which will prevent the internal npm install process from being kicked off by the Schematic. By doing this, I can then manually kick off npm install via child_process.spawn() and directly observe the stdout and stderr streams.
I'm curious if anyone knows of a way to spy on the stderr and stdout streams from the 'npm install' that's kicked off inside of my explicitly spawned ng new command?
Thanks!
If you're running on Linux you can use strace to spy on the ouput of another process.
strace -p7835 -e trace= -e write=3
See this answer for more details.
You can invoke strace from node of course, using spawn. To find the pid of the npm process (which is actually a node process btw), you would need to get the process tree using the ps command. There is already a node module that does this: ps-tree, which also appears to be cross platform.
For alternatives of strace on Windows, check this discussion. I would go for Process Monitor from Sysinternals.

View nodejs running scripts

Im using nightwatch and cucumberjs to make automated tests. After the execution I send a mail report of the status of the tests, but sometimes, when the tests completes it get stuck and doesnt send the mail report. I believe there is something inside my tests that doesn't close correctly but I don't know how I can see what is still running.
How can I display a list of the current processes that are still executing?
EDIT: Sorry for the confusion, by processes, I mean inside my nodejs program, not windows process
Linux:
Open termial.
Run the top command.
Windows:
Open task manager.
View running processes.
For Linux -
ps -ef | grep node
This will match all the processes with keyword node in it.
You can replace node with appropriate process name.
For Windows -
tasklist | FIND "node"

Jenkins won't kill node.exe?

I've got a Jenkins job set up to run a node.js server in the background, perform some tests on it (through a batch script, using Nightwatch), and then kill off the node server using the TaskKill batch command. Here's the command line script I have for the build:
START /B node ../app.js
runtests.bat
taskkill /F /IM node.exe
The build runs and passes, but it never seems to kill node. At the end of the console output I get:
Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
And I can see the node.exe process still running in my Task Manager.
If I run the same commands in my own command prompt it works fine, and kills node. It's just that Jenkins doesn't seem to execute that last command at all.
Any ideas? Am I maybe taking the wrong approach altogether?
Well I managed to get it working by installing the Hudson Post Build Task plugin, and just killing node in a post-build command. Still not sure why it wasn't working before though.

Is it possible to break into interactive mode in Node.js?

If you run node you get into interactive mode, you can inspect, execute functions etc. But once you run a function from interactive mode or from the OS shell, can you pause execution and go to interactive mode deliberately?
In browser you can use debugger keyword, but in Node.js debugger does nothing for me.
When my software fails and I want to quickly figure it out, I add a debugger instruction where I want the debugger to break and do:
node debug [script I want to debug]
Note that it is node debug, not node --debug. The latter also starts a debugger but it is one that waits on a port for some UI to connect.
Doing the above will cause the debugger to stop before executing your software. Type c to let it continue. It will next stop where you put your debugger statement. If you want to evaluate things dynamically you can use the repl command.
If you do it this way, you don't need any extra tools. The documentation is here.
try the node-inspector, you can debug your app with chrome developer tool
1.install and start node-inspector
npm install -g node-inspector
node-inspector &
2.start your app with debug parameter (if you want to break in the first line, replace the parameter to --debug-brk
node --debug server.js
3.open debug link in Chrome
open http://127.0.0.1:8080/debug?port=5858

Resources