如何在 axios 失败的 React 测试库中测试控制台输出的错误

问题描述 投票:0回答:0

我有一个学生组件

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

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

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

我的React测试如下:

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()???????
})

我的问题是:当我放置 debug() 时,我可以看到控制台输出中打印了“API 已失败”但是如何使用“expect”之类的东西来测试它?

reactjs jestjs enzyme react-testing-library
© www.soinside.com 2019 - 2024. All rights reserved.