Jest: How to throw an error inside a unhandledRejection handler - jestjs

I have the following code:
process.on('unhandledRejection', error => {
throw error;
});
But when I run jest without the --runInBand option this never throws, it just hangs on forever on the offending tests.
Any ideas on how to stop the process when an unhandledRejection is met?

Related

How to REALLY kill a child_process nodejs

I'm using mocha with Nodejs to test my restApi.
When I run mocha, I tell my test to create a child_process and run the API so I can make requests to it.
The problem is whenever the test exits (finishing or crashing), it seems that the API keeps running on background. I've seen some answers here that instructs to manually kill the child process whenever the main process exits. So I did it like this:
export function startProcess(done) {
const child = spawn('babel-node', ["app.js"]);
child.stdout.on("data", function(data) {
data = data.toString();
// console.log(data.toString());
if(data.indexOf("Server online") > -1) done();
});
child.stderr.on('data', function(err) {
console.log("ERROR: ", err.toString());
});
child.on('exit', function(code) {
console.log("PROPERLY EXITING");
console.log("Child process exited with code", code);
});
process.on('exit', function(code) {
console.log("Killing child process");
child.kill();
console.log("Main process exited with code", code);
});
}
When the main process exits it does log "Killing child process", meaning that child.kill() was indeed called. But if I try to run my test again, when the spawn command gets called, the API throws an error
Error: listen EADDRINUSE :::3300
, meaning that the API is still running and that port address is taken.
So I have to run sudo pkill node to really kill all node process and then npm test works again.
Am I missing something? Is this really the way to achieve what I'm expecting?
I thought about using child_process.exec to run sudo pkill node on my process.on('exit') listener, but that doesnt seem like a smart thing to do.
This is happening both in Mac and Ubuntu.
Any suggestions?
Thanks
"exit" is an event that gets triggered when node finishes it's event loop internally, it's not triggered when you terminate the process externally.
What you're looking for is executing something on a SIGINT.
Have a look at http://nodejs.org/api/process.html#process_signal_events

pm2 does not restart worker when express error occur

I'm using pm2 to manage process in my nodejs express application (running in cluster mode).
We had 2 kind of error handler
FIRST: 'uncaughtException' will be handled with
process.on('uncaughtException', function(err){});
Actually, I do not declare an handler like this cause of letting pm2 detect died worker in this case so restart the died worker automatically.
SECOND: express error handler, I mean the error will be forwarded to express error handler, not uncaughtException handler, the error handler like below
app.use(function(err, req, res, next) {})
I also do not declare this error handler for same purpose as uncaughtException.
But pm2 does not restart node in this case.
Any idea about this problem?
Many thanks
When catching errors with express error handler or even "uncaughtException" event, the process is still running, so pm2 won`t restart it.
If you want pm2 to restart after each exception, I would suggest something like this:
process.on('uncaughtException', function(e) {
console.log('An error has occured. error is: %s and stack trace is: %s', e, e.stack);
console.log("Process will restart now.");
process.exit(1);
})
Same goes for the express error handler. When we execute process.exit method, the process will terminate and pm2 will restart it.

gulp.watch task is throwing an error: (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR

This recently started happening in my gulp.watch task. It was working fine as of two weeks ago.
When I run $ gulp
when it gets to the watch task, it throws the following:
(FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-21)
events.js:85
throw er; // Unhandled 'error' event
^
Error: watch EMFILE
at exports._errnoException (util.js:746:11)
at FSEvent.FSWatcher._handle.onchange (fs.js:1157:26)
Per a few suggestions, I have updated node.js, with no luck.
There was also the suggestion that I was trying to watch too many files, so I changed the watch dir to a single file. Still throws the error.
Ive updated gulp to 3.9 globally and locally, along with my dependencies. I even rolled gulp back to an older version on both to see if that worked. No luck.
What could be throwing this on a gulp.watch task?
Here is my gulpfile task:
// Watch
gulp.task('watch', function() {
// Listen on port 35729
server.listen(35729, function (err) {
if (err) {
return console.log(err)
};
// Watch .scss files
gulp.watch('assets/styles/*.scss', ['styles']);
});
});
From the code you've posted, you've invoked gulp.watch but with no instruction as to what should happen when a file changes?
Normally a watch task would trigger the running of another task upon a file change. One would assume that watching changes in scss files should trigger scss compilation.
gulp.watch('assets/styles/style.scss', ['scss:compile']);
// where 'scss:compile' is another defined task for scss compilation.
Hope that helps you out and might give you some different output :)

Catch Errors in Protractor Runner

Is there a way to catch specific errors thrown directly by protractor's runner? For example, one uncaught exception is:
WARNING - pattern /var/www/smoran/repo/app/partials/app/app.controller.e2e.js did not match any files.
[launcher] Process exited with error code 1
/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:126
throw e;
^
Error: Spec patterns did not match any files.
at Runner.run (/var/www/smoran/repo/node_modules/protractor/lib/runner.js:249:11)
at TaskRunner.run (/var/www/smoran/repo/node_modules/protractor/lib/taskRunner.js:123:19)
at createNextTaskRunner (/var/www/smoran/repo/node_modules/protractor/lib/launcher.js:220:20)
at /var/www/smoran/repo/node_modules/protractor/lib/launcher.js:243:7
at _fulfilled (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:797:54)
at self.promiseDispatch.done (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:826:30)
at Promise.promise.promiseDispatch (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:759:13)
at /var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:573:44
at flush (/var/www/smoran/repo/node_modules/protractor/node_modules/q/q.js:108:17)
at process._tickCallback (node.js:419:13)
This error is thrown when protractor can not find any files using the globbing pattern specified or, possibly, when none of the files it finds that do exist can be parsed by jasmine or mocha.
Now, I do not care about the situation that causes this error. I do not care whether protractor can or can not find files with my globbing patterns, and if I do care, I would like to throw my own error(s), as its errors are much too verbose, being in fact uncaught exceptions. Unfortunately, protractor is tripping me up at every turn where I try to do that. It seems to be throwing the error in the runner, then catching it in its dependency, q, then rethrowing it if it is being run under NodeJS (which it is).
I am using gulp, and I am using child_process's spawn to spawn protractor, because all of gulp's protractor plugins seem to be blacklisted or advised not to be used. My code would be too long to show here, but it essentially ends up like this:
gulp.task('protractor', function() {
return spawn('protractor',
[
'--specs=' + notFoundTests
otherArgs
],
{
stdio: 'inherit'
});
});
I have protractor in my PATH, and notFoundTests is a string, not an array. I am using stdio: 'inherit' because I still want to see all the errors & output, but I am trying to find a way to filter it.
No matter what I try to catch, the error seems to not be emitted that way. It seems that it is not emitted as an error, so spawn(...).on('error', function() {}); does not work. Using a try catch block around the call to spawn also does not work. Even using process.on('uncaughtException'); does not catch it, even though it seems to be an uncaught exception.
My question is: How do I catch errors like this, that are thrown by protractor but not really returned? Does protractor provide any hooks, etc.? Also, why is it throwing this particular error? This seems to be an end-developer error, not something that protractor should handle itself.

Nodejs error has no traceback

I'm somewhat new to nodejs.
Using express / npm.
Once i hit '/' from my browser, I get this in output:
[TypeError: Object #<Object> has no method 'push']
No line numbers, no traceback, nothing.
I need to be able to fix this error, and to do that I need to know what file / line # this occurred.
How do I get nodejs to output a traceback?
First, add an express error handler, which is middleware expecting 4 arguments:
app.use(function (error, req, res, next) {
console.error(error.stack);
});
Make sure to put the above code at the very end of your middleware stack after all your normal middleware and route handlers are defined.
If that doesn't get you your stack trace, catch process-level uncaught exceptions:
process.on("uncaughtException", function (error) {
console.error(error.stack);
});
And one final tip would be run your app under the debugger with node --debug=3001 app.js, connect to it with node-inspector, open the debugger in chrome, enable "stop on exceptions", then trigger the error. The debugger will take you to the line where the exception occurs.

Resources