我设置了一个端点,该端点允许用户重置密码。一切正常,并且测试都通过了,直到我添加了nodemailer并添加了向用户发送电子邮件的行。
我正在使用Jest进行测试。
如果我注释掉测试通过的发送电子邮件的行,则currencyList : string[] = [];
ngOnInit() {
//form controls are initialized
this.handleCurrency();
}
handleCurrency(){
if(!this.formgroup.get('requestCurrency').value){
this.currencyService.fecthCurrencies().subscribe( (data) => {
this.currencyList = data;
this.defaultAndDisableCurrency();
}, () => {
//in case API call fails
this.currencyList.push("EUR");
this.defaultAndDisableCurrency();
});
}
//this.defaultAndDisableCurrency(); I would like to call the function here after API call is complete but this line gets executed without waiting for the call to complete
}
defaultAndDisableCurrency(){
let cur: any = this.formgroup.get('requestCurrency');
cur.valueChanges.subscribe((data)=>{
if(data === "EUR"){
cur.disable();
}
});
cur.setValue(this.currencyList[0]);
}
如果我保留该行-测试失败。我已经通过REST客户端确认一切正常,这使我相信测试是问题所在。
ReferenceError:Jest环境被拆除后,您正在尝试导入文件。
mailer.sendPasswordResetEmail(body.email, token);
我认为我没有正确嘲笑nodemailer-是否有人有使用nodemailer和开玩笑的经验?还是有更好的方法
有问题的文件是test("if a valid user requests a password change, they should be assigned a hash", async () => {
const userBefore = await helper.getUser();
expect(userBefore.passwordResetHash).toBe("");
await api
.post("/api/reset-password")
.send({ email: userBefore.email })
.expect(200);
const userAfter = await helper.getUser();
expect(userAfter.passwordResetHash).not.toBeNull();
});
,controllers/resetPassword.js
和utils/mailer.js
。
controllers / resetPassword.js
tests/resetPassword.test.js
utils / mailer.js
resetPasswordRouter.post("/", async (request, response) => {
// get email from request
const { body } = request;
// get the user with matching email
const user = await User.findOne({ email: body.email });
// if not found return error
if (!user) {
return response.status(400).json({ error: "User not found" });
}
// if user generate a token
const token = helper.generateToken();
// create a new user object with the resetPasswordHash defined
const update = {
passwordResetHash: await bcrypt.hash(token, 10),
};
// update user model with the password hash
const updatedUser = await User.findByIdAndUpdate(user.id, update, {
new: true,
});
mailer.sendPasswordResetEmail(body.email, token);
// setup timer to reset password hash in 30 minutes
setTimeout(async () => {
await User.findByIdAndUpdate(
user.id,
{ passwordResetHash: "" },
{ new: true }
);
}, 30000); // half hour
// return the updated user with the hash set
response.status(200).json(updatedUser);
});
您可以在这里找到存储库:https://github.com/gerrgg/gregpress
答案 0 :(得分:0)
您需要模拟函数mailer.sendPasswordResetEmail
,以便当控制器调用它时,它实际上会调用您的模拟实现。
test("if a valid user requests a password change, they should be assigned a hash", async () => {
const userBefore = await helper.getUser();
expect(userBefore.passwordResetHash).toBe("");
// Jest spy to intercept mailer.sendPasswordResetEmail call
let spy = jest.spyOn(mailer, 'sendPasswordResetEmail').mockImplementation(() => return true);
await api
.post("/api/reset-password")
.send({ email: userBefore.email })
.expect(200);
const userAfter = await helper.getUser();
expect(userAfter.passwordResetHash).not.toBeNull();
});