Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Stub nodemailer send method using sinon in mocha node.js

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.12k
    Comment on it

    Stub transport is useful for testing the email before actually sending email. It does not actually send an email, it convert mail object to a single buffer and return it with a send mail callback.

    In the below code, I have stubbed send method of nodemailer, using sinon module. Please check the below code:

    const sinon = require('sinon');
    const nodemailer = require('nodemailer');
    
    describe('Forgot password', () => {
        const middleware = server(0, true);
        const baseUrl = '/api/v1';
        let nm = '';
        let transport = '';
    
        beforeEach(cb => {
            transport = {
                name: 'testsend',
                version: '1',
                send: function send(data, callback) {
                    callback();
                },
                logger: false,
            };
            nm = nodemailer.createTransport(transport);
            cb();
        });
    
        context('when user exist in database', () => {
            it('repond with 200 request with correct code that exist', function test(done) {
                this.timeout(15000);
    
                sinon.stub(transport, 'send').yields(null, 'test');
    
                nm.sendMail({
                    subject: 'test',
                }, (err, info) => {
                    transport.send.restore();
                    done();
                });
    
                request(middleware)
                    .post(`${baseUrl}/recover-password`)
                    .send({
                        code: 'sss'
                    })
                    .set('Accept', 'application/json')
                    .expect('Content-Type', /json/)
                    .expect(httpConstants.STATUS_OK_200, {
                        message: 'Successfully done'
                    })
                    .end(done);
            });
        });
    });

     

    We have an alternative for the above code, that is using nodemailer-stub-transport module, but for that you have to modify your server side code, the code is as follows:
     

    var nodemailer = require('nodemailer');
    
    var stubTransport = require('nodemailer-stub-transport');
    
    
    var transport;
    if (/* are you testing */) {
        transport = nodemailer.createTransport(stubTransport());  
    } else {
        transport = nodemailer.createTransport(/* your normal config */);
     }
    
    
    module.exports = transport;

     

    You can set the mode (If it is in development or testing) using process global object as below:
     

    process.env.NODE_ENV= 'mocha_test';

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: