title: Jest: Mock one function in a class
body: <p>I'm very new to node and javascript, and trying to write a unittet with <code>jest</code> where I need to only mock 1 function of a class (and object).</p>
  
  <p>Here's a template I'm using:</p>
  
  <pre><code>// myModule.js
  class MyModule {
      constructor() {
        console.log("hello");
      }
      getCases(){
          return 2;
      }
      getOtherCases(){
          return 3;
      }
  }
  module.exports = MyModule;
  </code></pre>
  
  <p>and the test:</p>
  
  <pre><code>// myModule.test.js
  jest.unmock('./myModule.js');
  const myModule = require('./myModule.js');
  myModule.getCases = jest.fn();
  
  describe('Test mock function', () =&gt; {
      beforeAll(() =&gt; {
          myModule.getCases.mockImplementation(() =&gt; 32);
          mod = new myModule();
      });
  
      it('should pass', () =&gt; {
          console.log(mod.getCases());
          console.log(myModule.getCases());
      });
    });
  </code></pre>
  
  <p>The problem over here is <code>mod.getCases()</code> does not mock the function (<code>myModule.getCases()</code> does)</p>
  
  <pre><code>console.log myModule.test.js:12
  2
  
  
  console.log myModule.test.js:13
  32
  </code></pre>
  
  <p>Is there a specific way I need to create the object so it would mock the function?</p>