2017-07-31 15 views
1

私のテストでは、プロミスチェーンを消費するメソッドを待つことができません。プロミスチェーンを消費するJestテスト関数

は、ここでの方法

export class UserService { 
    static getUser(): void { 
    const isFetching = getUserIsFetching(storeInstance.getState()); 
    if (isFetching) { 
     return; 
    } 
    storeInstance.dispatch(fetchingUserActionCreator()); 
    HttpService.get<UserResponse>('/api/auth/user') 
     .then(userResponse => { 
     console.log('test 1'); 
     Promise.all([ 
      HttpService.get<Account>('/api/accounts'), 
      HttpService.get<PersonalDetails>('/api/accounts/personal-details'), 
     ]).then(responses => { 
      console.log('test 2'); 
      storeInstance.dispatch(fetchingUserSuccessActionCreator(responses)); 
      console.log('test 3'); 
     }) 
     }) 
     .catch(error => { 
     storeInstance.dispatch(fetchingUserFailActionCreator(error)); 
     }) 
    } 
} 

であり、これはテスト

test.only('should get the user', async() => { 
     await UserService.getUser(); 
     expect(getSpy).toHaveBeenCalledTimes(3); 
     expect(getSpy.mock.calls[0][0]).toBe('/api/auth/user'); 
     expect(getSpy.mock.calls[1][0]).toBe('/api/accounts'); 
     expect(getSpy.mock.calls[2][0]).toBe('/api/accounts/personal-details'); 
     expect(storeDispatchSpy).toHaveBeenCalledTimes(2); 
    }); 

それは右、問題はPromise.allの決意で得たassertationsための最初のものです。テストは決してそこにはなかった。

あなたが見ることができるようにいくつかのコンソールログを追加して、約束の解決がすべて呼び出されなかったかどうかを確認しました。すべてのコンソールログが返されました。

私のテストでは、await UserService.getUser()の直後にコンソールログを追加しましたが、これはテスト後に記録されたものです。

test 1 
await finished 
test 2 
test 3 

私はここで間違っていますか?

+1

'返品Promise.all(...)' < - 返品を忘れました – Jamiec

答えて

0

おそらく、ファイルにPromiseを含めていない可能性があります。

関連する問題