title: How to test an init() function in jest that is called from constructor and then calls other async functions
body: <p>I have the following code in javascript:</p>
  
  <pre class="lang-js prettyprint-override"><code>class SampleClass {
    constructor(param1) {
      this.param1 = param1;
      this.init();
    }
    async init() {
      await this.getData();
      this.loadIframe();
    }
    async getData() {
      const response = fetch(url);
      const data = response.json();
      //set the response to class variable
      this.param2 = data;
    }
    loadIframe() {
      //some logic to load iframe.
    }
  }
  </code></pre>
  
  <p>What would be the best approach to test this using jest?</p>
  
  <p>Currently I am testing the constructor logic by mocking the init() function.</p>
  
  <p>But <strong>I also have to test the getData()</strong> function. What should be the approach to test the getData() method.</p>
  
  <p>I tried testing the getData() function <strong>by not mocking the init() function and using async testing</strong>, but I am <strong>not sure</strong> <strong>where to use the await in the test</strong> as the function is nested and called from within the init which is called from constructor.</p>
  
  <pre><code>it('should fetch data', async()=&gt;{
     //some logic  
  })
  </code></pre>