Mocking email function in nodejs

前端 未结 4 1876
我寻月下人不归
我寻月下人不归 2021-02-05 09:29

I\'ve got a mailer function I\'ve built and trying to shore up the coverage. Trying to test parts of it have proven tricky, specifically this mailer.smtpTransport.sendMail

4条回答
  •  死守一世寂寞
    2021-02-05 10:10

    You can use a 'Stub' transport layer on your test instead of SMTP.

    var stubMailer = require("nodemailer").createTransport("Stub"),
        options = {
            from: "from@email.com",
            to: "to@email.com",
            text: "My Message!"
        };
    
       stubMailer.sendMail(options, function(err, response){
         var message = response.message;
       })
    

    So, in that case, 'message' will be the email in text format. Something like this:

    MIME-Version: 1.0
    X-Mailer: Nodemailer (0.3.43; +http://www.nodemailer.com/)
    Date: Fri, 25 Feb 2014 11:11:48 GMT
    Message-Id: <123412341234.e23232@Nodemailer>
    From: from@email.com
    To: to@email.com
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: quoted-printable
    
    My Message!
    

    For more examples, take a look at nodemailer test suite: https://github.com/andris9/Nodemailer/blob/master/test/nodemailer-test.js

提交回复
热议问题