title: Node - Stub nodemailer transport for tests
body: <p>I have a function I would like to test:</p>
  
  <pre><code>var mailServerOptions = {....};
  
  var transporter = nodemailer.createTransport(mailServerOptions);
  
  exports.sendTemplateEmail = (to, template, data) =&gt; {
      var mailOptions = {....}
  
      return new Promise((resolve, reject) =&gt; {
          transporter.sendMail(mailOptions, (err, result) =&gt; {
              if (err) {
                  return reject(err)
              }
  
              return resolve(result);
          });
      });
  }
  </code></pre>
  
  <p>How can I stub the transporter.sendMail in this situation? I found this <a href="https://stackoverflow.com/questions/20387034/mocking-email-function-in-nodejs/22016711#22016711">post</a> but it doesn't really fit what I'm trying to do.</p>
  
  <p>I can move the send part to it's own function and stub that if I really have to, but it would be nice if I didn't have to go that route.</p>