簡體   English   中英

Jest - 在方法中覆蓋 promise

[英]Jest - Cover promise within method

我有一個方法,在執行幾行代碼后調用 promise -

const handleDownload = () =>{
  
  if(!currentItem){
     return;
  }
  setDownloadState(true);
  setDeleteState(false);
  Task.DeleteAttachment(currentItem.FileId)
      .then((res)=>{
             if(res.status===200){
                   ...
                   ...
                   ...
             }
             else{
                dispatch(MarkError(true));
             }
          })
         .catch((error)=>{ 
                dispatch(ReportError(error));
               });
           }

.then((res)=>{之前的所有行都包含在測試覆蓋率報告中。但是 promise then 和 catch 沒有被覆蓋。

我怎樣才能覆蓋它們?

在您的測試用例中,您可以使用異步等待:

it('test', async() => {
  await request(app).post('/yourAPI').expect(200) // this route will call handleDownload
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM