Last active
September 1, 2017 19:39
-
-
Save badsyntax/7769526 to your computer and use it in GitHub Desktop.
Catching uncaught exceptions in grunt-jasmine-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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... | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This testing approach might provide a more elegant testing alternative:
https://stackoverflow.com/questions/46006944/how-to-expect-an-asynchronously-thrown-exception-in-jasmine-angular2-typescr