Just started using jest and the documentation doesn't seem to be too clear on mocking.
I have a module with the below code. If I want to test the send mail function, but not send an actual email out via mailgun what do I need to do here? I THINK in my test file I need to use the mock behaviour but can't work out how to specifically stop the mail going out, whilst also being able to check the right routes are followed e.g. invalid email address, error thrown, etc. Do I need to break this function down further?
const Mailgun = require('machinepack-mailgun');
const Emailaddresses = require('machinepack-emailaddresses');
const config = require('../Config');
// Function to send an email
const sendEmail = function (toaddress, toname, sub, msg, cb) {
// Validate the email address is correct and if so send an email. Otherwise log the error and exit.
Emailaddresses.validate({
string: toaddress,
}).exec({
error(err) {
return cb(err, null);
},
invalid() {
return cb(new Error('Email address is not valid.'), null);
},
success() {
Mailgun.sendPlaintextEmail({
apiKey: config.sender.apiKey,
domain: config.sender.domain,
toEmail: toaddress,
toName: toname,
subject: sub,
message: msg,
fromEmail: config.sender.fromEmail,
fromName: config.sender.fromName,
}).exec({
error(err) {
return cb(err, null);
},
success() {
return cb(null, 'Email Sent.');
},
});
},
});
};
module.exports.sendEmail = sendEmail;