Notary Public Mystifier


    1.0.0 • Public • Published


    nodemailer-mock Build Status Coverage Status Dependency Status Dev Dependency Status npm downloads

    Mocked nodemailer module for unit testing.


    npm install @eclass/nodemailer-mock --save-dev
    yarn add -D @eclass/nodemailer-mock

    mock api

    There are some special methods available on the mocked module to help with testing.

    • nodemailerMock.mock.reset()
      • resets the mock class to default values
    • nodemailerMock.mock.sentMail()
      • returns an array of sent emails
    • nodemailerMock.mock.shouldFailOnce()
      • will return an error on the next call to transport.sendMail()
    • nodemailerMock.mock.shouldFail(true|false)
      • indicate if errors should be returned for subsequent calls to transport.sendMail()
        • if true, return error
        • if false, return success
    • nodemailerMock.mock.mockedVerify(true|false)
      • 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.successResponse(success)
      • set the success message that is returned in the callback for transport.sendMail()
    • nodemailerMock.mock.failResponse(err)
      • set the err that is returned in the callback for transport.sendMail()


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

    const nodemailerMock = require('@eclass/nodemailer-mock');
    const transport = nodemailerMock.createTransport();
    // the email you want to send
    const email = ... // <-- your email here
    // send an email with nodestyle callback
    transport.sendMail(email, function(err, info) {
      if (err) {
        console.log('Error!', err, info);
      } else {
        console.log('Success!', info);
    // send an email with promises
    .then(function(info) {
      console.log('Success!', info);
    .catch(function(err) {
      console.log('Error!', err);
    // verify a transport
    transport.verify(function(err, success) {
      if (err) {
        console.log('Error!', err);
      } else {
        console.log('Success!', success);

    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.

    const should = require('should');
    const mockery = require('mockery');
    const nodemailerMock = require('@eclass/nodemailer-mock');
    describe('Tests that send email', function() {
      /* This could be an app, Express, etc. It should be 
      instantiated *after* nodemailer is mocked. */
      let app = null;
      before(function() {
        // 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(function() {
        // Reset the mock back to the defaults after each test
      after(function() {
        // Remove our mocked nodemailer and disable mockery
      it('should send an email using nodemailer-mock', function(done) {
        // call a service that uses nodemailer
        var response = ... // <-- your email code here
        // a fake test for something on our response'value');
        // get the array of emails we sent
        const sentMail = nodemailerMock.mock.sentMail();
        // we should have sent one email;
        // check the email for something
      it('should fail to send an email using nodemailer-mock', function(done) {
        // tell the mock class to return an error
        const err = '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', function(done) {
        // 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 @eclass/nodemailer-mock

    DownloadsWeekly Downloads






    Unpacked Size

    229 kB

    Total Files


    Last publish


    • eclass-bot
    • lgatica
    • enbonnet
    • segonzalezr
    • tsycl
    • asmaelrebo