@motowhere/nodemailer-mock-transport
v1.3.1
Published
mock-mailer for putting tests around services that use node-mailer
Downloads
4
Readme
nodemailer-mock-transport
Mock nodemailer-transport, for testing services that rely on nodemailer.
describe('mock-transport', function() {
it('should store configuration options so that they can be asserted against', function() {
var transport = mockTransport({
foo: 'bar'
});
transport.options.foo.should.equal('bar');
});
it('should store emails sent with nodemailer, so that they can be asserted against', function (done) {
var transport = mockTransport({
foo: 'bar'
})
var transporter = nodemailer.createTransport(transport)
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'hello',
text: 'hello world!'
}, function (err, info) {
/* Note that the callback includes the standard nodemail envelope and messageId props: */
if (err) throw err
info.messageId.should.not.be.a('null')
info.envelope.from.should.eql('[email protected]')
done()
})
transport.sentMail.length.should.equal(1)
transport.sentMail[0].data.to.should.equal('[email protected]')
transport.sentMail[0].message.content.should.equal('hello world!')
})
/* Here's how you can mock an arbritary failed send: */
it('should return an error and not send an email if there is no `to` in the mail data object', function () {
var transport = mockTransport({
foo: 'bar'
});
var transporter = nodemailer.createTransport(transport);
transporter.sendMail({
from: 'sender@address',
subject: 'hello',
text: 'hello world!'
});
transport.sentMail.length.should.equal(0);
});
});
it('should not send mail and throw and error if asked to do so', function () {
var transport = mockTransport({
foo: 'bar',
errorOnSend: { error: 'Failed to send for some reason!' }
})
var transporter = nodemailer.createTransport(transport)
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'hello',
text: 'hello world!'
})
transport.sentMail.length.should.equal(0)
})
})