How to check if a method is called in nested jest mock functions

Reza picture Reza · Jul 31, 2019 · Viewed 8.1k times · Source

I have a mock service like below

  const firebaseService = jest.fn(() => ({
    initializeApp: jest.fn(() => { /*do nothing*/}),
  }))

in my test I want to expect if initializeApp has been called. How can I check that?

it('should be called', () => {
   expect(???).toHaveBeenCalledTimes(1);
});

Update : Real scenario

  const collection = jest.fn(() => {
    return {
      doc: jest.fn(() => {
        return {
          collection: collection,
          update: jest.fn(() => Promise.resolve(true)),
          onSnapshot: jest.fn(() => Promise.resolve(true)),
          get: jest.fn(() => Promise.resolve(true))
        }
      }),
      where: jest.fn(() => {
        return {
          get: jest.fn(() => Promise.resolve(true)),
          onSnapshot: jest.fn(() => Promise.resolve(true)),
          limit: jest.fn(() => {
            return {
              onSnapshot: jest.fn(() => Promise.resolve(true)),
              get: jest.fn(() => Promise.resolve(true)),
            }
          }),
        }
      }),
      limit: jest.fn(() => {
        return {
          onSnapshot: jest.fn(() => Promise.resolve(true)),
          get: jest.fn(() => Promise.resolve(true)),
        }
      })
    }
  });

  const Firestore = {
    collection: collection
  }

    firebaseService = {
      initializeApp() {
        // do nothing
      },
      firestore: Firestore
    };

and I want to check below

 expect(firebaseService.firestore.collection).toHaveBeenCalled();
 expect(firebaseService.firestore.collection.where).toHaveBeenCalled();    
 expect(firebaseService.firestore.collection.where).toHaveBeenCalledWith(`assignedNumbers.123`, '==', true);

Answer

felixmosh picture felixmosh · Jul 31, 2019

You can define the inner spy as a variable.

const initializeAppSpy = jest.fn(() => { /*do nothing*/});

const firebaseService = jest.fn(() => ({
    initializeApp: initializeAppSpy,
}))

Then you can use the reference in order to expect on it:

it('should be called', () => {
   expect(initializeAppSpy).toHaveBeenCalledTimes(1);
});

EDIT You can create a mock to the entire service

const firebaseMock = {
   method1: 'returnValue1',
   method2: 'returnValue2'
}

Object.keys(firebaseMock).forEach(key => {
   firebaseMock[key] = jest.fn().mockReturnValue(firebaseMock[key]);
});

const firebaseService = jest.fn(() => firebaseMock);

now, you will have a firebaseMock object that all the methods are mocked. you can expect on each one of those methods.