title: Jest/Enzyme: How to handle an async function being called inside another?
body: <p>I am trying to run Jest/Enzyme async tests for <code>fetchTemperatures</code> and I've run into a bit of unfamiliar ground. If I define longitude and latitude and comment out <code>getUserCoordinates</code> I am able to pass my tests. Question is, how can I write my tests to allow/mock that an async function inside of <code>fetchTemperatures</code> is returning values?</p>
  
  <p>~ additionally, if there are any other issues with how the code is written please let me know, I am trying to warm back up to the js environment...</p>
  
  <p><strong>fetchTemperatures.js</strong></p>
  
  <pre><code>export const fetchTemperatures = async (format = 'weather', source = 'openWeather') =&gt; {
      // for 5-day-forecast set format arg to "forecast"
      try {
          let response;
          const { longitude, latitude } = await getUserCoordinates();
          //! the nested async above is preventing me from running the tests...
          // let longitude = 100;
          // let latitude = 100;
          if (source === 'openWeather') {
              response = await fetch(
                  `${openWeatherURL}/${format}?APPID=${process.env
                      .REACT_APP_OW_API_KEY}&amp;lat=${latitude}&amp;lon=${longitude}&amp;units=imperial`
              );
          } else if (source === 'darkSky') {
              response = await fetch(
                  `${darkSkyURL}${process.env
                      .REACT_APP_DS_API_KEY}/${latitude},${longitude}?exclude=[currently,minutely,hourly,flags]`
              );
          } else {
              //! is this getting hit? -- not sure
              throw new Error("Enter a valid source string. Ex: 'openWeather' or 'darkSky'");
          }
          return await response.json();
      } catch (error) {
          throw new Error('Fetch failed');
      }
  };
  </code></pre>
  
  <p><strong>fetchTemperatures.test.js</strong></p>
  
  <pre><code>
  describe('fetchTemperatures()', () =&gt; {
      let mockResponse = { data: 'Weather related data.' };
      let response;
      beforeAll(() =&gt; {
          window.fetch = jest.fn().mockImplementation(() =&gt; {
              return Promise.resolve({
                  ok: true,
                  json: () =&gt; Promise.resolve(mockResponse)
              });
          });
          //! Number of calls: 0 === FIGURE OUT NESTED ASYNC IN SOURCE
          response = fetchTemperatures();
      });
  
      // argument-based routes
      it('args=default, should call fetch with the correct url', () =&gt; {
          fetchTemperatures();
          expect(window.fetch).toHaveBeenCalledWith(
              `${openWeatherURL}/weather?APPID=${process.env
                  .REACT_APP_OW_API_KEY}&amp;lat=${latitude}&amp;lon=${longitude}&amp;units=imperial`
          );
      });
      it('args="weather", should call fetch with the correct url', () =&gt; {
          fetchTemperatures('forecast');
          expect(window.fetch).toHaveBeenCalledWith(
              `${openWeatherURL}/weather?APPID=${process.env
                  .REACT_APP_OW_API_KEY}&amp;lat=${latitude}&amp;lon=${longitude}&amp;units=imperial`
          );
      });
      it('args="weather", "darkSky", should call fetch with the correct url', () =&gt; {
          fetchTemperatures('weather', 'darkSky');
          expect(window.fetch).toHaveBeenCalledWith(
              `${darkSkyURL}${process.env
                  .REACT_APP_DS_API_KEY}/${latitude},${longitude}?exclude=[currently,minutely,hourly,flags]`
          );
      });
  
      // success
      it('Success::Promise Resolve: should return with the current weather', () =&gt; {
          response.then((results) =&gt; {
              expect(results).toEqual(mockResponse);
          });
      });
  
      // failure - success but response not ok
      it('Failure::Response !ok: should return an error', () =&gt; {
          window.fetch = jest.fn().mockImplementation(() =&gt; {
              return Promise.resolve({
                  ok: false
              });
          });
          expect(fetchTemperatures()).rejects.toEqual(Error('Fetch failed'));
      });
  
      // failure - reject
      it('Failure::Promise Rejects', () =&gt; {
          window.fetch = jest.fn().mockImplementation(() =&gt; {
              return Promise.reject(Error('Fetch failed'));
          });
          expect(fetchTemperatures()).rejects.toEqual(Error('Fetch failed'));
      });
  });
  
  </code></pre>