sendmail传输的回调从未在mocha的后挂钩中调用

时间:2016-11-06 22:50:34

标签: javascript unit-testing asynchronous mocha nodemailer

我想在所有执行测试后发送包含测试结果的电子邮件。

当我在挂钩中调用sendMail(nodemailer)时 - 它不起作用。

我的代码:

after(function(done) {
     sendReport();
     done();
});


function sendReport() {
   let mailOptions = {
        from: "xxx@gmail.com",
        to: "xxx@gmail.com",
        subject: "subject",
        text: "body Text",
        html: "<h2><b>TEXT.</b></h2>",
        attachments: [{
            path: "../reports/report.html"
        }]
    };

    let transporter = nodemailer.createTransport({
        service: "Gmail",
        auth: {
            user: "xxx@gmail.com",
            pass: "xxxx"
        }
    });

    transporter.sendMail(mailOptions, function (error, info) {
         if (error) {
            console.log(error);
        }
    });
}

2 个答案:

答案 0 :(得分:2)

发送电子邮件后执行done回调:

after(function(done) {
     sendReport(done);
});


function sendReport(done) {
   let mailOptions = {
        from: "xxx@gmail.com",
        to: "xxx@gmail.com",
        subject: "subject",
        text: "body Text",
        html: "<h2><b>TEXT.</b></h2>",
        attachments: [{
            path: "../reports/report.html"
        }]
    };

    let transporter = nodemailer.createTransport({
        service: "Gmail",
        auth: {
            user: "xxx@gmail.com",
            pass: "xxxx"
        }
    });

    transporter.sendMail(mailOptions, function (error, info) {
         if (error) {
            console.log(error);
        }
        done();
    });
}

答案 1 :(得分:0)

let mailTransport = nodemailer.createTransport(mailConfig);
mailTransport.sendMail(mailOptions, function(err, info){
    if (err) {
        console.log('ERRO');
        console.log(err.message);
        return process.exit(1);
    }
    console.log("messageId",info.messageId);
    console.log("envelope", info.envelope);
    console.log("accepted", info.accepted);
    console.log("rejected", info.rejected);
    console.log("pending", info.pending);
    console.log("response", response );
    console.log('SCSS', numero, mailOptions.to, termo);
});