I have a method in my node.js server, that returns a Promise - throws a custom Exception (UserNotAuthenticatedError) - and I want to write a test to make sure that this Exception is thrown whenever it has to.
The method is the following:
export function changePassword(userId, oldPass, newPass) {
var query = User.findById(userId);
return query.exec()
.then(user => {
if (user.authenticate(oldPass)) {
user.password = newPass;
return user.save();
} else {
// I want to test that this Exception is thrown
throw new UserNotAuthenticatedError();
}
});
}
I've tried writing the test, and this is what I have so far:
describe('#changePassword', function() {
it('should throw a UserNotAuthenticatedError when passing wrong password', function() {
var userId = user._id;
var wrongPwd = 'wrongpassword';
var newPwd = 'new password';
// so far the following only tells me that the Promise was rejected, but
// I WANT TO TEST THAT THE REJECTION WAS DUE TO A 'UserNotAuthenticatedError'
UserService.changePassword(userId, wrongPwd, newPwd).should.be.rejected;
});
});
I have accomplished checking that the promise returned was rejected, but I also want to be more specific and check that the returned promise is a UserNotAuthenticatedError. How could I do that?