title: How to test axios both success and failure case?
body: <p>I have a React application. I am using React-testing-library for testing. I am trying to write test cases to cover my axios failure case but not able to.</p>
  
  <p>To test, I have created a folder called <code>src/__mocks__</code> and which contains a file called 'axios.js'. The content inside is -</p>
  
  <pre><code>export default {
      get: jest.fn(() =&gt; Promise.resolve({ data: "success" }))
  };
  </code></pre>
  
  <p>In my component.test.js test file, I am able to test the success scenario of the axios call. But not sure how to test the failure case. Content is - </p>
  
  <pre><code>it("Test axios call", () =&gt; {
      axiosMock.get.mockResolvedValueOnce({
      data: { greeting: "hello there" }
      });
  });
  </code></pre>
  
  <p>Note - The axios call inside my component is under </p>
  
  <pre><code>useEffect(()=&gt;{
      axios.get('url')
      .then((res)=&gt;{console.log(res)})
      .catch((err)=&gt;{console.log(err)});
  },[])
  </code></pre>
  
  <p>And also note, that I am able to test the failure case by changing the Promise.resolve to Promise.reject inside the axios mock. But, at any point in time, I am able to test either, not both!</p>