title: Jest/Typescript: Mock class dependencies containing private members in jest and typescript
body: <p>Context : I have class B which is dependent on class A. I want to test a method of class B which is internally calling a method of class A. Now, I want to unit test my method of class B by mocking class A.</p>
  <p><strong>Note 1</strong>: <strong>Class A has some private members</strong></p>
  <p><strong>Note 2</strong>: <strong>Class A has no interface</strong></p>
  <p>Here's my code structure:</p>
  <pre><code>
  class Base {
  
    someMethod() {
       return &quot;Hello &quot;;
    }
  
  }
  
  
  class A {
  
    private _baseClassImpl: Base;
  
    constructor(baseClassImpl: Base) {
       this._baseClassImpl = baseClassImpl;
    }
  
    getSomething() {     
       return this._baseClassImpl.someMethod() + &quot; Something&quot;;
    }
  }
  
  
  class B {
    constructor(objectOfClassA: A) {
        this._objectOfClassA = objectOfClassA;
  
   }
  
   functionOfClassBToTest() {
       const returnValueFromClassA = this._objectOfClassA.getSomething();
  
       return returnValueFromClassA;
   }
  }
  
  </code></pre>
  <p>What I have tried so far :</p>
  <p>After taking suggestions from <a href="https://stackoverflow.com/questions/70635377/jest-typescript-mock-class-dependencies-in-jest-and-typescript">my previous SO post</a> I tried to write the tests like this:</p>
  <pre><code>
  const getSomethingMock = jest.fn().mockImplementation(() =&gt; {
      return &quot;Mock value&quot;;
  });
  
  const mockA = jest.Mocked&lt;A&gt; = {
     getSomething: getSomethingMock
  };
  
  test(&quot;test functionOfClassBToTest&quot;, () =&gt; {
     const classBToTest = new B(mockA);
     
     expect(classBToTest.functionofClassBToTest.toStrictEqual(&quot;Hello Something&quot;);
  
  });
  
  </code></pre>
  <p>The above code gives me this error:</p>
  <pre><code>Type '{ getSomething: jest.Mock&lt;any, any&gt;; }' is not assignable is not assignable to type 'Mocked&lt;A&gt;'. 
  Type '{ getSomething: jest.Mock&lt;any, any&gt;; }' is missing the following properties from type 'A': _baseClassImpl
  </code></pre>
  <p>And When I try to provide the mock value for _baseClassImpl like this:</p>
  <pre><code>const baseClassMock = jest.Mocked&lt;Base&gt; = {
    someMethod: jest.fn()
  };
  
  const getSomethingMock = jest.fn().mockImplementation(() =&gt; {
      return &quot;Mock value&quot;;
  });
  
  const mockA = jest.Mocked&lt;A&gt; = {
     getSomething: getSomethingMock,
     _baseClassImpl: baseClassMock
  };
  </code></pre>
  <p>Typescript gives me this error:</p>
  <pre><code>Type '{ getSomething: jest.Mock&lt;any, any&gt;; _baseClassImpl: jest.Mocked&lt;Base&gt;; }' is not assignable is not assignable to type 'Mocked&lt;A&gt;'. 
  
  Type '{ getSomething: jest.Mock&lt;any, any&gt;; _baseClassImpl: jest.Mocked&lt;Base&gt;; }' is not assignable is not assignable to type 'A'. 
  
  Property '_baseClassImpl' is private in type 'A' but not in type '{ getSomething: jest.Mock&lt;any, any&gt;; _baseClassImpl: jest.Mocked&lt;Base&gt;; }'
  </code></pre>
  <p>I have tried the answer on my <a href="https://stackoverflow.com/questions/70635377/jest-typescript-mock-class-dependencies-in-jest-and-typescript">previous SO post</a> but it didn't work incase of class with private members.</p>
  <p>Some notes:</p>
  <p><strong>Note 1</strong>: <strong>Class A contains private member</strong></p>
  <p><strong>Note 2</strong>: <strong>Class A has no interface</strong></p>
  <p><strong>Note 3</strong>: <strong>I don't want to initialize an object of class A inside my test function. I only want to mock the class.</strong></p>