title: Testing nested function in Jest
body: <p>I have in <strong>file.js</strong> code which I simplify this way:</p>
  
  <pre><code>function Apple(,data) {
    this.attr1 = data.attr1,
    this.attr2 = data.attr2,
    this.doSomething(data.flag);
  }
  
  Apple.prototype = new function() {
    this.function1 = function() {
      // do something
    }
    this.doSomething = function(flag) {
      // return value
    }
  }
  </code></pre>
  
  <p>I want to test <code>function1()</code>, for that, I want to mock first <code>doSomething()</code> to return a specific value, but I am failing because any call to <code>Apple()</code> function executes immediately <code>doSomething()</code>:</p>
  
  <pre><code>describe('Apple', () =&gt; {
      test('test function1()', () =&gt; {
          Apple.doSomething = jest.fn().mockImplementation((2) =&gt; {
              // do something;
          });
  
          let apple = new Apple(data); // the original doSomething() executes here instead of the mocked version
      });
  });
  </code></pre>
  
  <p>How can I achieve my goal ?</p>