I am having an issue with a expected [Function] to throw Error
message in my tests. Looking this up the suggestion is to add a catch block to my promises but I do not want to add catch blocks everywhere just so I can keep throwing the same exception. My catch will be in the final code (illustrated in my working test at bottom). Code works fine but fails to pass the desired commented out test about thrown errors.
I am not sure where the issue is - is it something I am missing in my promise or is it related to expect
?
My code:
// Common wrapper calling function used by other methods
MyClass.prototype.doCall = function(func) {
// Create a promise
var p = new P(function(resolve, reject) {
// Call async function - might return an error message - we want to throw an error for thay
mws.client.invoke(func, function(r) {
if (data.ErrorResponse) {
// Throwing error here doesn't seem to make it cascade at all - just reject message
return reject(data.ErrorResponse.Error.Message);
}
resolve(data);
});
}).
catch(function(e) {
// Have to throw here
throw new Error(e);
});
return p.delay(timeout);
};
// Here's the method I call in my tests
MyClass.prototype.fetchOrders = function(from, to) {
var self = this;
return this.doCall(o)
.then(function(orders) {
// Probably not related to issue but here just in case
return P.map(orders, function(r) {
return self.fetchOrderById(r.OrderId);
});
});
};
My test:
// This works fine - I can catch the error in my final code which is what I want
order.fetchOrders()
.then(function(orders) {
expect(orders).to.have.length(1);
done();
}).catch(function(e) {
console.log(e);
done();
})
// On the other hand this fails with: expected [Function] to throw Error
expect(order.fetchOrderList.bind(order, from, to)).to.throw(Error);
Aucun commentaire:
Enregistrer un commentaire