title: Jest test promise resolution and event loop tick
body: <p>I have a failing Jest test case for code where I'm using promises. It looks like the resolution of the promise is happening after the test has completed, meaning I can't check that my promise resolution code has been executed.</p>
  
  <p>It feels like I need to make the event loop tick so the promise is resolved and the resolution code is executed, but haven't found anything that can do that in Jest.</p>
  
  <p>Here's a sample case. The code to be tested:</p>
  
  <pre><code>const Client = require('SomeClient');
  
  module.exports.init = () =&gt; {
    Client.load().then(() =&gt; {
      console.log('load resolved');
  
      setTimeout(() =&gt; {
        console.log('load setTimeout fired, retrying init');
        module.exports.init();
      }, 1000);
    });
  };
  </code></pre>
  
  <p>Test code:</p>
  
  <pre><code>jest.useFakeTimers();
  
  const mockLoad = jest.fn().mockImplementation(() =&gt; Promise.resolve());
  
  jest.mock('SomeClient', () =&gt; {
    return {
      load: mockLoad
    };
  }, { virtual: true });
  
  const promiseTest = require('./PromiseTest');
  
  describe('SomeClient Promise Test', () =&gt; {
    it('retries init after 10 secs', () =&gt; {
      promiseTest.init();
  
      expect(mockLoad).toHaveBeenCalledTimes(1);
      expect(setTimeout).toHaveBeenCalledTimes(1); // &lt;-- FAILS - setTimeout has not been called
  
      jest.runAllTimers();
  
      expect(mockLoad).toHaveBeenCalledTimes(2);
    });
  });
  </code></pre>
  
  <p>The <code>expect(setTimeout).toHaveBeenCalledTimes(1);</code> assertion fails (<code>setTimeout</code> has not been called at all), I think because the promise has not yet been resolved.</p>
  
  <p>Am I doing something wrong here? Can I cause the event loop to tick inside the test?</p>