jeudi 30 juin 2016

Catching a function's error thrown from inner, anonymous function

I have this function:

Item.edit = function(item, newData, done) {
  Item.getUpdatedData(item, newData, function(updatedData) {
     if (this.hasValidData(updatedData)) {
       done(updatedData);
     } else {
       throw "Cannot update item";
     }
  });
}

I want to test that it throws an error under the right circumstances, but when I call the outer function in my test, I get an Unhandled rejection Error in the output (meaning the error is actually thrown), but it doesn't recognize that the function under test actually threw the error.

I'm pretty sure this has to do with the scope where the error is thrown, but I can't figure out how to throw the error from the outer scope of the function.

Aucun commentaire:

Enregistrer un commentaire