I have a method on my node.js server that returns Promise - throws a custom exception ( UserNotAuthenticatedError) - and I want to write a test to make sure that this Exception is thrown whenever necessary.
The method is as follows:
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 {
throw new UserNotAuthenticatedError();
}
});
}
I tried to write a test, and this is what I still have:
describe('#changePassword', function() {
it('should throw a UserNotAuthenticatedError when passing wrong password', function() {
var userId = user._id;
var wrongPwd = 'wrongpassword';
var newPwd = 'new password';
UserService.changePassword(userId, wrongPwd, newPwd).should.be.rejected;
});
});
I performed a check that the promise was returned was rejected, but I also want to be more specific and check that the returned promise is this UserNotAuthenticatedError. How can i do this?
source
share