Testing a Promise using setTimeout with Jest : <p>I'm trying to understand Jest's asynchronous testing.</p>

<p>My module has a function which accepts a boolean and returns a Promise of a value. The executer function calls <code>setTimeout</code>, and in the timed out callback the promise resolves or rejects depending on the boolean initially supplied. The code looks like this: </p>

<pre><code>const withPromises = (passes) =&gt; new Promise((resolve, reject) =&gt; {
    const act = () =&gt; {
    console.log(`in the timout callback, passed ${passes}`)
        if(passes) resolve('something')
        else reject(new Error('nothing'))
    }

    console.log('in the promise definition')

    setTimeout(act, 50)
})

export default { withPromises }
</code></pre>

<p>I'd like to test this using Jest. I guess that I need to use the mock timers Jest provides, so my test script looks a bit like this: </p>

<pre><code>import { withPromises } from './request_something'

jest.useFakeTimers()

describe('using a promise and mock timers', () =&gt; {
    afterAll(() =&gt; {
        jest.runAllTimers()
    })


    test('gets a value, if conditions favor', () =&gt; {
        expect.assertions(1)
        return withPromises(true)
            .then(resolved =&gt; {
                expect(resolved).toBe('something')
            })
    })
})
</code></pre>

<p>I get the following error/failed test, whether or not I call <code>jest.runAllTimers()</code></p>

<pre><code>Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
</code></pre>

<p>Can you explain where I'm going wrong and what I might do to get a passing test that the promise resolves as expected?</p>
