Miss any of our Open RFC calls?Watch the recordings here! »


1.5.3 • Public • Published


nodemailer-mock Build Status Code Coverage Dependency Status Dev Dependency Status Downloads

Mocked nodemailer module for unit testing.


npm install nodemailer-mock --save-dev
yarn add -D nodemailer-mock

mock api

There are some special methods available on the mocked module to help with testing. They are under the .mock key of the mocked nodemailer.

  • nodemailerMock.mock.reset()
    • resets the mock class to default values
  • nodemailerMock.mock.getSentMail()
    • returns an array of sent emails during your tests, since the last reset
  • nodemailerMock.mock.setShouldFailOnce()
    • will return an error on the next call to transport.sendMail()
  • nodemailerMock.mock.setShouldFail({boolean} shouldFail)
    • indicate if errors should be returned for subsequent calls to transport.sendMail()
      • if true, return error
      • if false, return success
  • nodemailerMock.mock.setShouldFailCheck({Function} (email)=>{})
    • indicate if the specific email passed to the function should fail the call to transport.sendMail()
      • if function returns true, return error
      • if function returns false, return success
  • nodemailerMock.mock.setMockedVerify({boolean} isMocked)
    • determine if a call to transport.verify() should be mocked or passed through to nodemailer
      • if true, use a mocked callback
      • if false, pass through to a real nodemailer transport
  • nodemailerMock.mock.setSuccessResponse({Mixed} success)
    • set the success message that is returned in the callback for transport.sendMail()
  • nodemailerMock.mock.setFailResponse({Error} err)
    • set the Error that is returned in the callback for transport.sendMail()

Note that the .mock methods in previous versions are aliased to the new names.

Version 1.5+ returns an Error object on error rather than a string.


The mocked module behaves in a similar fashion to other transports provided by nodemailer.

setup test

const nodemailerMock = require('nodemailer-mock');
const transport = nodemailerMock.createTransport();

// the email you want to send
const email = ... // <-- your email here

use nodestyle callbacks

// send with nodestyle callback
transport.sendMail(email, function(err, info) {
  if (err) {
    return console.log('Error!', err, info);
  return console.log('Success!', info);

// verify with nodestyle callback
transport.verify(function(err, success) {
  if (err) {
    return console.log('Error!', err);
  return console.log('Success!', success);

use promises

// send with promises
  .then(function(info) {
    console.log('Success!', info);
  .catch(function(err) {
    console.log('Error!', err);

// verify with promises
  .then(function(success) {
    console.log('Success!', success);
  .catch(function(err) {
    console.log('Error!', err);

use async/await

// send an email with async / wait
try {
  const info = await transport.sendMail(email);
} catch (err) {
  console.log('Error!', err);

// verify with async / wait
try {
  const info = await transport.verify();
} catch (err) {
  console.log('Error!', err);

example using mocha and mockery

Here is an example of using a mocked nodemailer class in a mocha test using mockery. Make sure that any modules that require()'s a mocked module must be called AFTER the module is mocked or node will use the unmocked version from the module cache. Note that this example uses async/await. See the module tests for additional example code.

const should = require('should');
const mockery = require('mockery');
const nodemailerMock = require('nodemailer-mock');

describe('Tests that send email',  async () {

  /* This could be an app, Express, etc. It should be 
  instantiated *after* nodemailer is mocked. */
  let app = null;

  before(async () {
    // Enable mockery to mock objects
      warnOnUnregistered: false,
    /* Once mocked, any code that calls require('nodemailer') 
    will get our nodemailerMock */
    mockery.registerMock('nodemailer', nodemailerMock)
    ### IMPORTANT! ###
    /* Make sure anything that uses nodemailer is loaded here, 
    after it is mocked just above... */

  afterEach(async () {
    // Reset the mock back to the defaults after each test
  after(async () {
    // Remove our mocked nodemailer and disable mockery
  it('should send an email using nodemailer-mock', async () {
    // call a service that uses nodemailer
    const response = ... // <-- your email code here
    // a fake test for something on our response
    // get the array of emails we sent
    const sentMail = nodemailerMock.mock.getSentMail();
    // we should have sent one email
    // check the email for something
  it('should fail to send an email using nodemailer-mock', async () {
    // tell the mock class to return an error
    const err = new Error('My custom error');
    // call a service that uses nodemailer
    var response = ... // <-- your code here
    // a fake test for something on our response
  it('should verify using the real nodemailer transport', async () {
    // tell the mock class to pass verify requests to nodemailer
    // call a service that uses nodemailer
    var response = ... // <-- your code here
    /* calls to transport.verify() will be passed through, 
       transport.sendMail() is still mocked */


npm i nodemailer-mock

DownloadsWeekly Downloads






Unpacked Size

36.9 kB

Total Files


Last publish


  • avatar