title: How to test failed request with axios / jest
body: <p>I've created a very small app that calculates the total price to pay for certain crypto if you pass in the coin and quantity. I want to test for errors but I am always getting &quot;Received promise resolved instead of rejected&quot;. I believe this is due to the fact that if the url is wrong axios still resolves the promise.</p>
  <p>The second problem I have is that I have tried testing if the url is correct but I have an issue that headers are part of the url and I can't see to figure out how to test just the url path without headers.</p>
  <p>Those are tests 2 and 3 (Test number 1 is working)</p>
  <pre class="lang-js prettyprint-override"><code>// getTotalPrice.js
  const axios = require(&quot;axios&quot;);
  
  let symbol = process.argv[2];
  let quantity = process.argv[3];
  
  const API = `https://rest.coinapi.io/v1/exchangerate`;
  const headers = {headers:{&quot;X-CoinAPI-Key&quot;: &quot;MY TOKEN&quot;},};
  
  const getTotalPrice = async (symbol = &quot;BTC&quot;, quantity = 1) =&gt; {
    try {
    let res = await axios.get(`${API}/${symbol}/USD`, headers);
    let data = res.data;
    return Math.round(data.rate * quantity * 100) / 100;
    } catch(err) {
      console.log(err)
    }
  };
  
  getTotalPrice(symbol, quantity);
  
  module.exports = {
    getTotalPrice,
    API
  };
  
  // getTotalPrice.test.js
  
  const { get, getError } = require(&quot;axios&quot;);
  const { getTotalPrice } = require(&quot;../getTotalPrice&quot;);
  describe(&quot;getTotalPrice&quot;, () =&gt; {
    afterEach(() =&gt; {
      get.mockClear();
    });
    it(&quot;fetches data successfully from an api&quot;, async () =&gt; {
      const res = {
        data: {
          rate: 34000,
        },
      };
      get.mockImplementation(() =&gt; Promise.resolve(res));
      await expect(getTotalPrice()).resolves.toEqual(res.data.rate);
    });
    it(&quot;throws an error when incorrect data is passed&quot;, async () =&gt; {
      const errorMessage = &quot;Wrong inputs passed in&quot;;
  
      getError.mockImplementationOnce(() =&gt; Promise.reject({}));
      await expect(getTotalPrice()).rejects.toThrowError();
    });
    it(&quot;uses correct url&quot;, async () =&gt; {
      const data = {
        data: {
          rate: 2000,
        },
      };
      get.mockImplementationOnce(() =&gt; Promise.resolve(data));
  
      await getTotalPrice(&quot;ETH&quot;);
      expect(get).toHaveBeenCalledWith(
        `${API}/ETH/USD`
      );
    });
  });
  
  // axios.js (in __mocks__)
  
  module.exports = {
    get: jest.fn(() =&gt; Promise.resolve({ data: {} })),
    getError: jest.fn(() =&gt; Promise.reject()),
  };
  </code></pre>
  <p>To recap, test 1 passes, test 2 fails with &quot;Received promise resolved instead of rejected&quot; and test 3 fails as I am getting headers as well not just url.</p>