title: How to test img.onload using Jest?
body: <p>I'm stuck with <code>img.onload</code> event testing. I know that this is an async operation and it should be maybe mocked, but I still couldn't figure out how to solve the problem. I've also seen a couple of similar cases, but they are different from this one.</p>
  
  <p>Previously visited:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/44462665/how-do-you-use-jest-to-test-img-onerror">How do you use Jest to test img.onerror</a></li>
  <li><a href="https://stackoverflow.com/questions/43251302/how-to-test-asnyc-code-with-jest-or-test-image-onload-with-jsdom">How to test asnyc code with Jest ( or test &quot;image.onload&quot; with jsdom )</a></li>
  <li><a href="https://stackoverflow.com/questions/47273782/how-do-i-test-image-onload-using-jest-in-the-context-of-redux-actions-or-othe">How do I test `image.onload` using jest in the context of redux actions (or other callbacks assigned in the action)</a></li>
  </ul>
  
  <p>Code to test:</p>
  
  <pre><code>  function funcToTest(img, callback) {
      const img = new Image()
      img.src = img
  
      img.onload = () =&gt; {
        callback(true) // should return callback with true on finish
      }
  
      img.onerror = (e) =&gt; {
        callback(false) // should return callback with false on error
        console.log(e) 
      }
    }
  
    funcToTest()
  </code></pre>
  
  <p>Testing environment:</p>
  
  <pre><code>describe('tet it', () =&gt; {
    it('test', done =&gt; {
      const callback = status =&gt; {
        expect(status).toEqual(true) // but nothing happen
        done()
      }
  
      funcToTest('some_image', callback)
    })
  })
  </code></pre>
  
  <p>Also I get an error on the finish:</p>
  
  <pre><code>    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
          &gt; 2 |   it('test', done =&gt; {...
  
  </code></pre>
  
  <p>Thanks in advance!</p>