Skip to content Skip to sidebar Skip to footer

Stub A Standalone Module.exports Function Using Rewire

I am trying to stub a module.exports function. But I have some trouble. I will give you a sudo code of the situation. MyController.js const sendOTPOnPhone = rewire('../../src/servi

Solution 1:

Here is the integration testing solution using proxyquire, you should use Globally override require.

app.js:

const express = require('express');
const controller = require('./controller');

const app = express();
app.get('/api/v1/auth/otp/generate', controller);

module.exports = app;

controller.js:

let sendOTPOnPhone = require('./sendOTPOnPhone');

module.exports = asyncfunction(req, res) {
  const { error, data } = awaitsendOTPOnPhone(req.query.phone);
  if (error) return res.send(error);
  return res.send(data);
};

sendOTPOnPhone.js:

module.exports = asyncfunction(phone) {
  const result = awaitfetch(`external-api-call`);
  if (result.status !== 'success')
    return {
      error: 'Failed to send OTP!',
      data: null,
    };
  return {
    error: null,
    data: result,
  };
};

sendOTP.test.js:

const request = require('supertest');
const sinon = require('sinon');
const proxyquire = require('proxyquire');

describe('GET /api/v1/auth/otp/generate', function() {
  it('should generate OTP', async () => {
    let stub = sinon.stub().resolves({
      error: null,
      data: { message: 'OTP sent' },
    });
    stub['@global'] = true;
    const app = proxyquire('./app', {
      './sendOTPOnPhone': stub,
    });
    const result = awaitrequest(app)
      .get('/api/v1/auth/otp/generate?phone=8576863491')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200);
    sinon.assert.calledOnce(stub);
    console.log(result.body);
  });
});

Integration test results with coverage report:

  GET /api/v1/auth/otp/generate
{ message: 'OTP sent' }
    ✓ should generate OTP (2373ms)


  1 passing (2s)

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |   68.75 |       25 |      50 |   73.33 |                   
 app.js            |     100 |      100 |     100 |     100 |                   
 controller.js     |   83.33 |       50 |     100 |     100 | 5                 
 sendOTPOnPhone.js |      20 |        0 |       0 |      20 | 2-4,8             
-------------------|---------|----------|---------|---------|-------------------

source code: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/60599945

Solution 2:

I have answered a similar question here stub SMS otp method (without using external dependecy like proxyquire)

basically, the problem is with exports. Move your sendOtp method to a generalized place e.g User Model or schema. Import model, stub its function. It should work fine.

you are stubbing imported property called sendOtp instead of original function.

Post a Comment for "Stub A Standalone Module.exports Function Using Rewire"