sendmail传输的回调从未在mocha的after钩子中调用



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

当我在after钩子中调用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);
        }
    });
}

发送邮件后执行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();
    });
}
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);
});

最新更新