title: How to test dynamic imports (then and catch) with Jest
body: <p>So I have been struggling with how to test dynamic imports generally and in this case, especially in jest, I have been reading a lot on the internet but did not find anything concrete, so I thought about bringing the question up to centralize a decent solution.</p>
  <p>I have the following methods inside of a class</p>
  <pre><code>class MyClass {
     successMethod() {  ...  }
  
     errorMethod() { ... }
  
      myMethod() {
          return import('./myFile.js')
             .then(() =&gt; this.successMethod())
             .catch(() =&gt; this.errorMethod());
      }
  }
  </code></pre>
  <p><strong>My question is:</strong></p>
  <p>How do you mock both Success and Failing promise cases for this dynamic import using Jest to make sure each method (<code>successMethod</code> and <code>errorMethod</code>) are called when resolving or failing respectively?.</p>
  <p>I found <code>jest.doMock</code> helps for mocking the resolved case but did not find a way to make the dynamic import fail by mocking it so the <code>catch</code> case is uncovered.</p>
  <p><strong>Note:</strong> this is not a react application, this is a Vanilla JS project.</p>