title: Best Way to Test Promises in Jest
body: <p>Unless I'm misunderstanding something, the resolves and rejects (<a href="https://facebook.github.io/jest/docs/expect.html#resolves" rel="noreferrer">https://facebook.github.io/jest/docs/expect.html#resolves</a>) won't be available until vNext. What is the recommended way now/in the meantime to test promises with Jest? Is it just putting expects in the thens and catches?</p>
  <p>For example:</p>
  <pre><code>describe('Fetching', () =&gt; {
      const filters = {
          startDate: '2015-09-01'
      };
      const api = new TestApiTransport();
  
      it('should reject if no startdate is given', () =&gt; {
          MyService.fetch().catch(e =&gt; expect(e).toBeTruthy()); // see rejects/resolves in v20+
      });            
  
      it('should return expected data', () =&gt; {
          MyService.fetch(filters, null, api).then(serviceObjects =&gt; {
              expect(serviceObjects).toHaveLength(2);
          }).catch(e =&gt; console.log(e));
      });            
  });
  </code></pre>
  <p><strong>UPDATE 15 June 2019</strong>: Not too long after I posted this question, Jest started supporting this out of the box. I changed the accepted answer below to reflect the currently best way to do this.</p>
  <p><strong>UPDATE 8 Dec 2021</strong>: At some point Jest started supporting async/await. So while other methods noted work, I've taken to simply (for most cases) using something like:</p>
  <pre class="lang-js prettyprint-override"><code>it('should do something', async () =&gt; {
      const expected = true; 
      expect(await funcToTest()).toEqual(expected);
  });
  </code></pre>
  <p>As with most cases, async/await is much more readable than alternatives. The only case I use <code>resolves</code> or <code>rejects</code> now is for simple cases like:</p>
  <pre class="lang-js prettyprint-override"><code>it('should not throw when doing something', async () =&gt; {
      await expect(funcToTest()).resolves.not.toThrow();
  });
  
  it('should throw when something is wrong', async () =&gt; {
      await expect(funcToTest()).rejects.toThrow();
  });
  </code></pre>