Skip to content

Instantly share code, notes, and snippets.

@caligo-mentis
Created June 9, 2016 07:30
Show Gist options
  • Save caligo-mentis/784c4ebe847d80b8fab2ff6e6f1b117f to your computer and use it in GitHub Desktop.
Save caligo-mentis/784c4ebe847d80b8fab2ff6e6f1b117f to your computer and use it in GitHub Desktop.
Test uncaught exception with mocha tests runner
var should = require('should');
describe('uncaught exception', function() {
describe('with interception', function() {
var listeners;
beforeEach(function() {
listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
});
afterEach(function() {
listeners.forEach(function(listener) {
process.on('uncaughtException', listener);
});
});
it('should be handled', function(done) {
process.once('uncaughtException', function(err) {
should(err.message).eql('Unhandled');
done();
});
setImmediate(function() {
throw new Error('Unhandled');
});
});
});
describe('without interception', function() {
// Would produce duplicated tests
it('should not be handled', function(done) {
process.once('uncaughtException', function(err) {
should(err.message).eql('Unhandled');
done();
});
setImmediate(function() {
throw new Error('Unhandled');
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment