Mocking email function in nodejs - node.js

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
var nodemailer = require('nodemailer')
var mailer = {}
mailer.smtpTransport = nodemailer.createTransport('SMTP', {
'service': 'Gmail',
'auth': {
'XOAuth2': {
'user': 'test#test.com',
'clientId': 'googleClientID',
'clientSecret': 'superSekrit',
'refreshToken': '1/refreshYoSelf'
}
}
})
var mailOptions = {
from: 'Some Admin <test#tester.com>',
}
mailer.verify = function(email, hash) {
var emailhtml = 'Welcome to TestCo. Click this '+hash+''
var emailtxt = 'Welcome to TestCo. This is your hash: '+hash
mailOptions.to = email
mailOptions.subject = 'Welcome to TestCo!'
mailOptions.html = emailhtml
mailOptions.text = emailtxt
mailer.smtpTransport.sendMail(mailOptions, function(error, response){
if(error) {
console.log(error)
} else {
console.log('Message sent: '+response.message)
}
})
}
I'm unsure of how to go about testing, specifically ensuring that my mailer.smtpTransport.sendMail function is passing the correct parameters without actually sending the email. I'm trying to use https://github.com/whatser/mock-nodemailer/tree/master, but I'm probably doing it wrong. Should I be mocking out the method?
var _ = require('lodash')
var should = require('should')
var nodemailer = require('nodemailer')
var mockMailer = require('./helpers/mock-nodemailer')
var transport = nodemailer.createTransport('SMTP', '')
var mailer = require('../../../server/lib/account/mailer')
describe('Mailer', function() {
describe('.verify()', function() {
it('sends a verify email with a hashto an address when invoked', function(done) {
var email ={
'to': 'dave#testco.com',
'html': 'Welcome to Testco. Click this bleh',
'text': 'Welcome to Testco. This is your hash: bleh',
'subject': 'Welcome to Testco!'
}
mockMailer.expectEmail(function(sentEmail) {
return _.isEqual(email, sentEmail)
}, done)
mailer.verify('dave#testco.com','bleh')
transport.sendMail(email, function() {})
})
})

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

You can directly mock the sendMail function but it's not obvious how to access it from the tests. A Mailer instance is returned when you create a transport so you need to directly import that class in to your test.
const Mailer = require('nodemailer/lib/mailer')
Then you can mock or stub the sendMail method on the prototype in the usual way. Using Jasmine, you can do it like this:
beforeEach(function () {
spyOn(Mailer.prototype, 'sendMail').and.callFake(function (mailOptions, cb) {
cb(null, true)
})
})
The callFake ensures that the sendMail's callback is still executed encase you need to test what happens next. You can easily simulate an error by passing a first argument to cb: cb(new Error('Email failed'))
Now that the mock is set up, you can check everything is working as intended:
expect(Mailer.prototype.sendMail).toHaveBeenCalled()

expectEmail simply hooks into the transport layer, and expects you to identify the email ( return true if this is the email you are expecting ) by looking at the sentEmail contents.
In this case, return sentEmail.to === 'dave#testco.com' should suffice.
Keep in mind however, this module was designed in an environment where tests are ran in a random order and concurrently. You should propably randomize your data heavily to prevent collisions and false positives. BTW we use something like: var to = Date.now().toString(36) + Faker.Internet.email();

This example works fine for me
======== myfile.js ========
// SOME CODE HERE
transporter.sendMail(mailOptions, (err, info) => {
// PROCESS RESULT HERE
});
======== myfile.spec.js (unit test file) ========
const sinon = require('sinon');
const nodemailer = require('nodemailer');
const sandbox = sinon.sandbox.create();
describe('XXX', () => {
afterEach(function() {
sandbox.restore();
});
it('XXX', done => {
const transport = {
sendMail: (data, callback) => {
const err = new Error('some error');
callback(err, null);
}
};
sandbox.stub(nodemailer, 'createTransport').returns(transport);
// CALL FUNCTION TO TEST
// EXPECT RESULT
});
});

Related

How do you query data using Mongoose within a service function?

I have a service in my node application where I am trying to use nodemailer to send an email with a link to clients. The issue I am having is that when I try to query the recipient email address using Mongoose in the service I get this response message in the console log Promise { <pending> } and obviously my email does not send because I am not returning an email address. I am using the code below as the service that is called from my controller. It looks like everything is working fine in the controller because when I use a test email address as the recipient email the message is sent and function returns no errors. Am I writing the function to query the email wrong or is there a better way I should be setting this up?
var nodemailer = require('nodemailer');
const Obrf = require("../models/Obrf");
const sendLink = (x) => {
var id = x;
const offerid = id.toString();
const url = 'http://localhost:3000/offerletter/' + offerid
async function returnEmail (y) {
const email = await Obrf.findById(y, 'email_address').exec();
return email;
};
const sendToEmail = returnEmail(id);
console.log(sendToEmail);
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'sender#gmail.com',
pass: 'password'
}
});
var mailOptions = {
from: 'sender#gmail.com',
to: sendToEmail,
subject: 'Congratulations on your offer!',
text: 'Go to: ' + url + ' to sign your offer letter!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};
module.exports = {
sendLink,
};
async before a function makes the function return a promise:
are you sure if Obrf.findById(y, 'email_address').exec() returning promise ?
if not remove the aysnc await
function returnEmail (y) {
const email = Obrf.findById(y, 'email_address').exec();
return email;
};
const sendToEmail = returnEmail(id);
if its returning the promise then do something like this
const sendToEmail = returnEmail(id).then(id=>id);

creating an observable from transporter.sendMail

i am trying to create an observable from sendMail, currently my test for that looks like that:
/// <reference path="../../typings/index.d.ts" />
import * as chai from "chai";
let assert = chai.assert;
const Rx = require('rxjs');
var nodemailer = require('nodemailer');
describe("emailPlugin", function() {
it.only('should emit one value from a callback', function () {
let mailOptions = {
from: '"Fred Foo 👥" <MY#gmail.com>', // sender address
to: 'YOUR#gmail.com', // list of receivers; comma seperated
subject: 'Hello ✔', // Subject line
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
let transporter = nodemailer.createTransport('smtps://MY%40gmail.com:MY#smtp.gmail.com');
let boundCallback = Rx.Observable.bindNodeCallback(transporter.sendMail);
boundCallback(mailOptions, function(error, info){
if(error){
return error;
}
return info.response;
}).subscribe(x => console.log(x), e => console.error(e));
});
});
the result is an error:
[TypeError: Cannot convert undefined or null to object]
can someone help me out here?
I had exactly the same problem as you, I found this fixed it. Instead of:
let boundCallback = Rx.Observable.bindNodeCallback(transporter.sendMail);
This works for me:
let boundCallback = Rx.Observable.bindNodeCallback((options: any, callback: any) => {
return transporter.sendMail(options, callback);
});
I have no idea why, it seems to me the original form should be correct. Looks like TypeScript struggles with the type of the sendMail function. Hope that helps!

How to stub a nodejs "required" constructor using sinon?

I'm writing unit tests for a method that uses the email-templates module like this:
var EmailTemplate = require('email-templates').EmailTemplate;
module.exports = {
sendTemplateEmail: function (emailName, data, subject, to, from) {
var template = new EmailTemplate(__dirname + "/../emails/" + emailName);
data.from = FROM;
data.host = config.host;
return template.render(data)
.then(function (result) {
return mailer.sendEmail(subject, to, from, result.html, result.text);
})
.then(function () {
log.info(util.format("Sent %s email to %s. data=%s", emailName, to, JSON.stringify(data)));
return Promise.resolve();
})
.catch(function (err) {
return Promise.reject(new InternalError(err, "Error sending %s email to %s. data=%s", emailName, to, JSON.stringify(data)));
});
}
};
The unit test looks like this:
var assert = require("assert"),
sinon = require("sinon"),
Promise = require("bluebird"),
proxyquire = require("proxyquire");
describe('mailer#sendTemplateEmail', function () {
var templates,
template;
beforeEach(function() {
templates = {
EmailTemplate: function(path) {}
};
template = {
render: function(data) {}
};
sinon.stub(templates, "EmailTemplate").returns(template);
});
it("should reject immediately if template.render fails", function () {
const TO = {email: "user1#example.com", first: "User"};
const FROM = {email: "user2#example.com", first: "User"};
const EMAIL_NAME = "results";
const SUBJECT = "Results are in!";
const DATA = {
week: 10,
season: "2015"
};
var err = new Error("error");
var mailer = proxyquire("../src/mailer", {
"email-templates": templates
});
sinon.stub(template, "render").returns(Promise.reject(err));
return mailer.sendTemplateEmail(EMAIL_NAME, DATA, SUBJECT, TO, FROM)
.then(function () {
assert.fail("Expected a rejected promise.");
})
.catch(function (err) {
assert(err.message === "error");
assert(mailer.sendEmail.notCalled);
});
});
};
The problem I'm encountering is on the first line of the sendTemplateEmail function which instantiates a new EmailTemplate object. The EmailTemplate constructor being called points to the non-stub EmailTemplate function defined in the beforeEach, rather than the sinon stub created on the last line of the beforeEach. If I evaluate the require('email-templates').EmailTemplate statement, however, it correctly points to the sinon stub. I'd prefer not to have to change my code to call the require statement inline like:
var template = new require('email-templates').EmailTemplate(__dirname + "/../emails/" + emailName);
Is there any way to accomplish the stub the way I'm intending?
You can inject your dependency when you construct your mailer - exp:
function mailer(options) {
options = options || {};
this.email_template = options.email_template;
}
Then in the sendTemplateEmail function - use the email_template member.
Also - not sure about your mailer code - but if you need your mailer to act as a singleton in your code (and it isn't already) - you can add this to your mailer:
module.exports = {
getInstance: function(emailTemplate) {
if(this.instance === null){
this.instance = new mailer(emailTemplate);
}
return this.instance;
}
}
Then when you require your mailer you can use the syntax:
var template = new require('email-templates').EmailTemplate(__dirname + "/../emails/" + emailName);
var mail = mailer.getInstance(template);
This way your application (unit test framework or your actual/real-world application) will determine the type of mailer that will be used for the lifetime of the process.

nodejs exporting function email.send doesn't execute

Have mailer.js below but when importing 'var email = require('./models/mailer')' my 'email.send' doesn't execute ?
var config = require('./config');
var email = require('emailjs');
var server = email.server.connect({
host: config.smptRelay
});
module.exports = function send() {
server.send({
text: config.emailText,
from: config.emailFrom,
to: config.emailTo,
subject: config.emailSubject
}, function (err, message) { /*console.log(err || message); */
});
};
You're exporting the function directly, as module.exports, and not as a property of it. Just call it as email().

How to include an html email template in a node js application

I have an html template that we use to send to new website registrations. It is a simple html file that I would like to load into a variable so that I can replace certain parts before sending out using nodemailer (eg [FIRST_NAME]). I am trying to avoid having to paste a large chunk of html into my exports function. Any ideas on how I could go about doing that?
For a clearer idea, what I need to know is how to actually do this:
var first_name = 'Bob';
var html = loadfile('abc.html').replace('[FIRST_NAME]', first_name);
Here's an example of how to do it using ejs, but you can use any templating engine:
var nodemailer = require("nodemailer");
var ejs = require('ejs');
var transport = nodemailer.createTransport("SMTP", {
service: <your mail service>,
auth: {
user: <user>,
pass: <password>
}
});
function sendMail(cb) {
var user = {firstName : 'John', lastName: 'Doe'};
var subject = ejs.render('Hello <%= firstName %>', user);
var text = ejs.render('Hello, <%= firstName %> <%= lastName %>!', user);
var options = {
from: <from>,
replyTo: <replyto>,
to: <to>,
subject: subject,
text: text
};
transport.sendMail(options, cb);
}
To load the template file just us the fs module. Here's how to do it synchronously when the file is encoded in utf-8:
var fs = require('fs');
var template = fs.readFileSync('abc.html',{encoding:'utf-8'});
Maybe this will be useful for someone, as this question is already answered.
I'm working with jade and it was quite challenging figuring it out, at the end turned out to be very simple :)
(PS: this code is not optimized, is just an example)
Js part with nodemailer:
var nodemailer = require('nodemailer')
var jade = require('jade');
var config = {
// config for sending emails like username, password, ...
}
var emailFrom = 'this#email.com';
var emailTo = 'this#email.com';
var templateDir = 'path/to/templates/';
var transporter = nodemailer.createTransport(config);
var username = 'thisUsername'
// rendering html template (same way can be done for subject, text)
var html = jade.renderFile(templateDir+'/html.jade', {username: 'testUsername'});
//build options
var options = {
from: emailFrom,
to: emailTo,
subject: 'subject',
html: html,
text:'text'
};
transporter.sendMail(options, function(error, info) {
if(error) {
console.log('Message not sent');
console.log(info);
return false;
}
else{
console.log('Message sent: ' + info.response);
console.log(info);
return true;
};
});
html.jade
p test email html jade
p
| Username:
| !{username}
Here is the example for using email-templates and nodemailer.
js file:
var path = require('path');
var EmailTemplate = require('email-templates').EmailTemplate;
var transporter = nodemailer.createTransport(config);
var templateDir = path.join(__dirname, '/yourPath/emailTemplates', 'subdir');
var template = new EmailTemplate(templateDir)
var username = 'testUsername';
var transport = nodemailer.createTransport(config)
template.render(locals, function (err, results) {
if (err) {
return console.error(err)
}
// replace values in html template
console.log('template render')
console.log(err);
// default is results.html in this case
// read template and replace desired values
var res1 = results.html.toString();
var str = res1.replace('__username__', username);
console.log(str);
console.log('end template render')
transport.sendMail({
from: emailFrom,
to: emailTo,
subject: 'subject',
html: str,
text: results.text
}, function (err, responseStatus) {
if (err) {
return console.error(err)
}
console.log(responseStatus)
})
})
html.html
test email html
username:
<div>
__username__
</div>

Resources