How can I test chained promises in a Jest test?

前端 未结 2 1570
遇见更好的自我
遇见更好的自我 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'; } }
    })));
    
    0 讨论(0)
  • 2021-01-19 18:12

    Login actions › signIn should call firebase

    TypeError: auth.signInWithEmailAndPassword is not a function

    This tells that your store.dispatch(signIn(user.email, user.password)) fails, thus your second console.log won't go into your then chain, use catch or second callback argument of then instead.

    0 讨论(0)
提交回复
热议问题