Last active
October 22, 2015 22:22
-
-
Save antonlvovych/1013b8997d3e9f5a07fb to your computer and use it in GitHub Desktop.
Wraps and executes the passed function into try-catch
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
function tryCatch (/* fn, context, arguments */) { | |
var args = Array.prototype.slice.call(arguments); | |
var fn = args[0]; | |
var context = args[1]; | |
args = args.slice(2, args.length); | |
try { | |
fn.apply(context, args); | |
} catch (e) { | |
} | |
} |
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
function error(e) { | |
throw e; | |
} | |
error = sinon.spy(error); | |
tryCatch(error, null, new Error('Exception was thrown')); | |
expect(error) | |
.to.have.been.calledOnce | |
.and | |
.have.been.calledWithMatch({ message: 'Exception was thrown', name: 'Error' }) | |
.and | |
.have.thrown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment