1

I am a newbie to javascript.I am trying to write a basic unit test using proxyquire. for below code.

file A:

const modA=require('./modA');
const modB=require('./modB');
const async=require('async');
module.exports=function(a,b,callback){

async.parallel([
   function(callback){
      // db call
       modA(a,b,callback);
   },
   function(callback){
       // db call
       mobB(a,b,callback);
   }
],
//not able to test code
(err,res){
     //do something    
});

};

Unit test for file A looks like below:

const proxyquire=require('proxyquire');


function modaAStub(a, b, callback) {
  return (null, modAresponse);
}

function modaBStub(a, b, callback) {
  return (null, modaBresponse);
}

describe('test suite', () => {
  it('test: should return results', (done) => {
    const fileA = proxyquire('../../fileA', {
      './modA': modaAStub,
      './modB': modaBStub
    });

    fileA(someinput1,someinput2);
  done();
  });
});

the problem is,I am unable to figure out how to test the piece of code in fileA which has '//do something'.

Appreciate any pointers/code.

Thanks.

2 Answers2

1

Ok,I have been too stupid.The unit test for fileA will look like below.

    const proxyquire=require('proxyquire');
    const async=require('async');
    const expect=require('chai').expect;

    const modAresponse={
      a:'1'
     };
     const modBresponse={
      b:'1'
     };
    const a={
      a:'1'
     };
     const b={
      b:'1'
     };
     const someresponse={
      a:'1',
      b:'1'
     };
    function modaAStub(a, b, callback) {
      return callback(null, modAresponse);
    }

    function modaBStub(a, b, callback) {
      return callback(null, modaBresponse);
    }

    describe('test suite', () => {
      it('test: should return results', (done) => {
        const fileA = proxyquire('../../fileA', {
          './modA': modaAStub,
          './modB': modaBStub,
           async
        });

        fileA(a,b,(err,response)=>{
                  expect(JSON.stringify(response)).to.equal(JSON.stringify(someresponse));
        });
      done();
      });
    });
0

You can pass in another function, and use that as the callback.

function callbackStub(err, result) {
    return (null, modAresponse);
}

then in your file A:

// import the callback stub above
async.parallel([
function(callback){
     // db call
       modA(a,b,callback);
   },
   function(callback){
       // db call
       mobB(a,b,callback);
   }
],callbackStub);
Josh
  • 334
  • 1
  • 17
  • I am confused.I am trying to write test for fileA. if I change my file A I am changing my actual code.I may be missing something. – user1948304 Mar 05 '17 at 00:00
  • I don't really understand what you're trying to test then -- in theory, the test should verify the the .parallel function returns the correct outputs to the callback. If you want to test the callback seperately, there's no need to even have it be part of the parallel logic, you can just mock the input as the input to it. – Josh Mar 05 '17 at 00:23
  • you can just mock the input as the input to it--can you elaborate more on this.The problem I have is the modA,modB are db calls.So I just wanted them to be stubbed out as to what the output will be and verify just the mock result.This is just to test out the logic in //do something.I do not want the actual running of the async itself. – user1948304 Mar 05 '17 at 01:00
  • 1
    Thank you Josh.For the pointer.I have been too stupid to understand what you have been saying. – user1948304 Mar 05 '17 at 06:45