在服务器上运行时,邮件可以从Postman / swagger发送,但是在尝试运行单元测试用例时,则无法发送。
/// user.spec.js
it('should able to send email with token to reset password',async ()=>{
try {
var result = await broker.call('user.forgotPassword',
testData.forgotPasswordWithValidDetails);
expect(result.message).toBe('Email has been sent to registered
Email Id');
} catch (error) {
throw error;
}
})
/// user.service.js
if (userExist) {
/// creates a token so we can verify
var token = await this.generateJWT(userExist);
let subject = 'Password reset link';
let text = 'Hello,\n\n' + 'Please reset your password by clicking the
link: \nhttp:\/\/' + 'localhost:3000' + '\/updatePassword\/' + token;
eventEmitter.emit('sendEmail', subject, userExist, text);
return {sucess:true,message:"Email has been sent to registered Email
Id"};
/// event.js
var mailOptions = {
from: process.env.SENDER_EMAIL_ID,
to: user.email,
subject: subject,
text: text,
}
var response = await transporter.sendMail(mailOptions);
resolve(response);
}
单元测试用例成功执行后,应发送电子邮件。