2017-07-31 36 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); 
    }); 

首先對assertations它這樣做是正確的,問題是在Promise.all的決心。測試從未到達那裏。

我添加了一些控制檯日誌,你可以看到,檢查是否所有的承諾的決心都沒有被調用,但沒有。所有的控制檯日誌都被返回。

然後我在測試中添加了一個控制檯日誌,在await UserService.getUser()之後,這是測試後記錄的內容。

test 1 
await finished 
test 2 
test 3 

我在做什麼錯在這裏?

+1

'return Promise.all(...)'< - 你忘了返回 – Jamiec

回答

0

這可能是你沒有在文件中包含Promise。