How can I test chained promises in a Jest test?

前端 未结 2 1596
遇见更好的自我
遇见更好的自我 2021-01-19 17:43

Below I have a test for my login actions. I\'m mocking a Firebase function and want to test if the signIn/signOut functions are called.

The

2条回答
  •  长情又很酷
    2021-01-19 18:10

    You have two issues here,

    The tests pass however I do not see my 2nd console log. Which is this line console.log('store ==>', store);.

    That is because the test is not waiting for the promise to fulfill, so you should return it:

    it('signOut should call firebase', () => {
        console.log('signOut should call firebasew');
        return store.dispatch(signOut()).then(() => { // NOTE we return the promise
          expect(mockFirebaseService).toHaveBeenCalled();
          console.log('store ==>', store);
          expect(store.getActions()).toEqual({
            type: USER_ON_LOGGED_OUT
          });
          console.log('END');
        });
    
      });
    

    You can find examples in the Redux official documentation.

    Secondly, your signIn test is failing because you have mocked the wrong firebase:

    jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));
    

    That should probably look more like:

    jest.mock('services/firebase', () => new Promise(resolve => resolve({
        signInWithEmailAndPassword: () => { return { getIdToken: () => '123'; } }
    })));
    

提交回复
热议问题