在运行单元测试用例时触发事件时无法发送邮件

时间:2019-08-24 13:43:29

标签: javascript jestjs dom-events nodemailer

在服务器上运行时,邮件可以从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);
}

单元测试用例成功执行后,应发送电子邮件。

0 个答案:

没有答案