Home > Software engineering >  Jest - Cover promise within method
Jest - Cover promise within method

Time:11-18

I have a method where promise is called after execution of few lines of code -

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));
               });
           }

All the lines before .then((res)=>{ are getting covered in test coverage report. But the promise then and catch are not getting covered.

How can I cover them?

CodePudding user response:

In your test case you can use async await:

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

  • Related