Skip to content

Instantly share code, notes, and snippets.

@antonlvovych
Last active October 22, 2015 22:22
Show Gist options
  • Save antonlvovych/1013b8997d3e9f5a07fb to your computer and use it in GitHub Desktop.
Save antonlvovych/1013b8997d3e9f5a07fb to your computer and use it in GitHub Desktop.
Wraps and executes the passed function into try-catch
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) {
}
}
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