Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active September 1, 2017 19:39
Show Gist options
  • Save badsyntax/7769526 to your computer and use it in GitHub Desktop.
Save badsyntax/7769526 to your computer and use it in GitHub Desktop.
Catching uncaught exceptions in grunt-jasmine-node
/*
Fuck! This has been doing my head in.
The problem: In your jasmine specs, you introduce an error in your async method callback handler,
and all you get is "Fatal error: Object #<Object> has no method 'nullMethod' (for example).
So there's no freekin stack trace to track down where this error occurred.
*/
/*
So the issue here is you can't catch errors in async handlers because by the time the async handler
is called, the try {} catch {} block is complete, for example:*/
try {
setTimeout(function() {
throw new Error('Test');
});
} catch(e) {
// Will not get here...
alert('Error occured!');
}
/*
It appears jasmine-node (https://github.com/mhevery/jasmine-node) can handle uncaught exceptions
via a 'captureExceptions' option, (and hooking onto process.on('uncaughtException')) but this is
only works when running jasmine from the bin file. Thus, there's no way to catch uncaughtException's
in the grunt-jasmine-node module, so you gotta do it yourself!
*/
/* An example Gruntfile.js */
module.exports = function(grunt) {
// Catch unhandled exceptions and show the stack trace. This is most
// useful when running the jasmine specs.
process.on('uncaughtException',function(e) {
grunt.log.error('Caught unhandled exception: ' + e.toString());
grunt.log.error(e.stack);
});
// etc...
};
@dpmott
Copy link

dpmott commented Sep 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment