1 year ago

#220601

test-img

Arjun Prajapati

How to ignore library method call count in mocha and super test?

// function to be tested 

async function attachFile(req, res) {
  const requestPayload = new FormData();
  const fileName = req.swagger.params.name.value
  const description = req.swagger.params.description.value;
  const email = req.swagger.params.email.value;
  const customerId = req.swagger.params.customerId.value;


  requestPayload.append('ticketId', req.swagger.params.ticketId.value);
  requestPayload.append('file', req.swagger.params.file.value.buffer, `${customerId}_${fileName}`);
  requestPayload.append('description', `${customerId}_${description}`);

Try {
// call third party API with data 
}catch(error){
 // handle exception case 
}

}


// test case  details 


   beforeEach(() => {
    sandbox.restore();
    formDataAppendSpy = sandbox.spy(FormData.prototype, 'append');
  });
 


      it('should return an ID in response', async () => {
        return request
          .post('/v1/api/attachments')
          .set('Content-Type', 'multipart/form-data')
          .query({ email, customerId })
          .field('ticketId', ticketId)
          .field('description', description)
          .field(‘name’,’sampleName’)
          .expect(200)
          .then(() => {
            expect(formDataAppendSpy).to.be.calledWith('ticketId', ticketId);
            expect(formDataAppendSpy).to.be.calledWith(‘name', ‘sampleName’);
            expect(formDataAppendSpy).to.be.calledWith('description', `${customerId}_${description}`);
            expect(formDataAppendSpy).to.be.callCount(3)

          });
      }).timeout(TIMEOUT); 

I am trying to do testing of API using mocha along with super test , chai and sinon. I want to track(spy) the formdata.append method which is used in api call to create form data, which will be submitted to further system for process.

I have used 3 parameter so it should be called thrice only but is getting executed 6 times. When i started looking at problem i come to know that when we call below line of code of super agent it also calls formdata.append method interally.

request.field('key',value)

Do we have any way to resolve this i dont want to count method call when it is invoked from library module ?

node.js

mocha.js

superagent

spy

sinon-chai

0 Answers

Your Answer

Accepted video resources