0

I have a students component

function Student(props)
{
   const [studentData,setStudentData]=useState([]);

   function getStudentData()
     {
        axios
            .get("myAPI/getStudentData")
            .then {}
            .catch(error=>
                   {
                       console.log(error);
                     }

      }
     useEffect(()=>{
                 getStudentData(); 
                },[]);
}

my React test is as follows:

it('failure', async()=>
{
   const api = Promise.reject('API has failed');
   axios.get.mockImplementation(()=> api);
   render(<Student />);
   
   ///Here how to test if I am getting 'API has failed' in console ?
   expect('API has failed').toBeInTheDocument()???????
})

My question is: When I put debug(), I can see 'API has failed' is printed in console output But how to test it using something like 'expect'?

V R
  • 133
  • 1
  • 8
  • 2
    I think this one https://stackoverflow.com/questions/49096093/how-do-i-test-a-jest-console-log is quite similar to you question. – funnyDev Mar 18 '23 at 16:21
  • @funnyDev: Thank you. Appreciate your help. Yes it is partially as I need to capture console log error in axios. – V R Mar 19 '23 at 01:23
  • Does this answer your question? [Jest: mocking console.error - tests fails](https://stackoverflow.com/questions/44596915/jest-mocking-console-error-tests-fails) – Avi Mar 20 '23 at 08:41

0 Answers0