0

I'm using Gmail API, Now I try to stub gmail.users.threads.list method for test cases. But it doesn't work.

I tried:

const { google } = require('googleapis');                                                               
const gmail = google.gmail('v1');      
const sinon = require('sinon');   
const { Module } = require('path-to-testing-module');        
describe('Module testing', () => {
  sinon.stub(gmail.users.threads, 'list').returns('ObjectWantToReturn');
  it('should call someFunction', () => {
    Module.someFunction(); //Cannot call stubbed function inside the module
  });
  
})                

It can't call stubbed function

UPDATE

I found problem. Problem is with the destructing of the objects.

Below code is worked for me.

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

describe('Module testing', () => {
  const googleStub = {
    google: {
      gmail: sinon.stub().returns({
        users: {
          threads: {
            list: sinon.stub().returns('ObjectWantToReturn')
          }
        }
      })
    }
  };

const { Module } =  proxyquire('./path-to-testing-module', {
    'googleapis': googleStub
  });

  it('should call someFunction', () => {
    Module.someFunction(); //Call stubbed function
  });
});

Now i am confused, what the difference between importing with require and proxyquire. The destructing can't affect while testing with proxyquire.

Surya S
  • 53
  • 8

0 Answers0