title: Jest: How to mock one specific method of a class
body: <p>Let's suppose I have the following class: </p>
  
  <pre><code>export default class Person {
      constructor(first, last) {
          this.first = first;
          this.last = last;
      }
      sayMyName() {
          console.log(this.first + " " + this.last);
      }
      bla() {
          return "bla";
      }
  }
  </code></pre>
  
  <p>Suppose I want to create a mocked class where method 'sayMyName' will be mocked and method 'bla' will stay as is.</p>
  
  <p>The test I wrote is: </p>
  
  <pre><code>const Person = require("../Person");
  
  jest.mock('../Person', () =&gt; {
      return jest.fn().mockImplementation(() =&gt; {
          return {sayMyName: () =&gt; {
              return 'Hello'
          }};
      });
  });
  
  
  let person = new Person();
  test('MyTest', () =&gt; {
      expect(person.sayMyName()).toBe("Hello");
      expect(person.bla()).toBe("bla");
  })
  </code></pre>
  
  <p>The first 'expect' statement passes, which means that 'sayMyName' was mocked successfully. But, the second 'expect' fails with the error: </p>
  
  <blockquote>
    <p>TypeError: person.bla is not a function</p>
  </blockquote>
  
  <p>I understand that the mocked class erased all methods.
  I want to know how to mock a class such that only specific method(s) will be mocked.</p>