title: How can I mock a private property in a class I'm trying to test in jest
body: <p>I have a class method I want to test:</p>
  <pre><code>setStepResolution(resolution: stepResolution): void {
          switch (resolution) {
              case stepResolution.FULL_SETUP:
                  this.stepperMotors.left.ms1Pin.digitalWrite(0)
                  this.stepperMotors.left.ms2Pin.digitalWrite(0)
                  this.stepperMotors.left.ms3Pin.digitalWrite(1)
                  this.stepperMotors.right.ms1Pin.digitalWrite(0)
                  this.stepperMotors.right.ms2Pin.digitalWrite(0)
                  this.stepperMotors.right.ms3Pin.digitalWrite(1)
                  break
              case stepResolution.HALF_STEP:
                  this.stepperMotors.left.ms1Pin.digitalWrite(1)
                  this.stepperMotors.left.ms2Pin.digitalWrite(0)
                  this.stepperMotors.left.ms3Pin.digitalWrite(0)
                  this.stepperMotors.right.ms1Pin.digitalWrite(1)
                  this.stepperMotors.right.ms2Pin.digitalWrite(0)
                  this.stepperMotors.right.ms3Pin.digitalWrite(0)
                  break
  </code></pre>
  <p>Each of these <code>digitalWrite</code> calls is made to an instance of a different class that are created when my class is constructed:</p>
  <pre><code>export default class BotController {
  
      private stepperMotors: StepperMotorCollection
  
      constructor() {
          this.initalizeMotors()
      }
  
      private initalizeMotors(): void {
          this.stepperMotors = {
              left: {
                  directionPin: new Gpio(Number(process.env.LEFT_DIRECTION_PIN), { mode: Gpio.OUTPUT }),
                  stepPin: new Gpio(Number(process.env.LEFT_STEP_PIN), { mode: Gpio.OUTPUT }),
                  ms1Pin: new Gpio(Number(process.env.LEFT_RESOLUTION_PIN_MS1), { mode: Gpio.OUTPUT }),
                  ms2Pin: new Gpio(Number(process.env.LEFT_RESOLUTION_PIN_MS2), { mode: Gpio.OUTPUT }),
                  ms3Pin: new Gpio(Number(process.env.LEFT_RESOLUTION_PIN_MS3), { mode: Gpio.OUTPUT }),
                  stepsPerMM: Number(process.env.LEFT_STEPS_PER_MM),
                  swapCoils: Boolean(process.env.LEFT_SWAP_COILS),
              },
              right: {
                  directionPin: new Gpio(Number(process.env.RIGHT_DIRECTION_PIN), { mode: Gpio.OUTPUT }),
                  stepPin: new Gpio(Number(process.env.RIGHT_STEP_PIN), { mode: Gpio.OUTPUT }),
                  ms1Pin: new Gpio(Number(process.env.RIGHT_RESOLUTION_PIN_MS1), { mode: Gpio.OUTPUT }),
                  ms2Pin: new Gpio(Number(process.env.RIGHT_RESOLUTION_PIN_MS2), { mode: Gpio.OUTPUT }),
                  ms3Pin: new Gpio(Number(process.env.RIGHT_RESOLUTION_PIN_MS3), { mode: Gpio.OUTPUT }),
                  stepsPerMM: Number(process.env.RIGHT_STEPS_PER_MM),
                  swapCoils: Boolean(process.env.RIGHT_SWAP_COILS),
              },
          }
      }
  </code></pre>
  <p>I could create a mock for the <code>stepperMotors</code> property in my test with mocks of the <code>Gpio</code> class (I'm already mocking the constructor for some of the other tests):</p>
  <pre><code>test(&quot;can change step resolution&quot;, () =&gt; {
          // * The step resolution of the stepper motors can be changed via the code.
          // * The settings can be controlled by an enum that denotes each of the possible
          // * resolutions.
  
          const mockStepperMotorConfiguration: StepperMotorCollection = {
              left: {
                  directionPin: new pigpio.Gpio(1),
                  stepPin: new pigpio.Gpio(1),
                  ms1Pin: new pigpio.Gpio(1),
                  ms2Pin: new pigpio.Gpio(1),
                  ms3Pin: new pigpio.Gpio(1),
                  stepsPerMM: 1,
                  swapCoils: false,
              },
              right: {
                  directionPin: new pigpio.Gpio(1),
                  stepPin: new pigpio.Gpio(1),
                  ms1Pin: new pigpio.Gpio(1),
                  ms2Pin: new pigpio.Gpio(1),
                  ms3Pin: new pigpio.Gpio(1),
                  stepsPerMM: 1,
                  swapCoils: false,
              },
          }
  
          // ^ To change the resolution to a full step
          // * send in the full step enum
          newController.setStepResolution(stepResolution.FULL_SETUP)
  </code></pre>
  <p>But I can't because the <code>stepperMotor</code> property is private.</p>
  <p>There are several ways I could solve this (make the property public, make a public method for setting the property), but neither of them seems desirable because the property should never be accessible outside of the class so I would be exposing properties or methods only to support testing.</p>
  <p>Is there another way of doing this kind of test? I know in jest I can mock a method on a class in javascript by replacing the prototype function e.g.:</p>
  <pre><code>BotController.prototype.someMethod = jest.fn()
  const controller = new BotController
  </code></pre>
  <p>And if this was a class I was trying to mock I could pass in the properties as a mock implementation, e.g.:</p>
  <pre><code>jest.mock(&quot;../BotController&quot;, () =&gt; ({
      stepperMotors: mockStepperMotorConfiguration
  }))
  </code></pre>
  <p>But then everything else in the class would also be mocked and you'd loose the point.</p>
  <p>Any idea on how I should approach this?</p>
  <h1>Update: trying to create a backdoor</h1>
  <p>I'm trying out Taplar's approach of creating a backdoor.</p>
  <p>I tried casting my controller instance as an <code>any</code>:</p>
  <p><a href="https://i.stack.imgur.com/DSjJG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DSjJG.png" alt="making a backdoor" /></a></p>
  <p>But the complier is still yelling at me:</p>
  <p><a href="https://i.stack.imgur.com/qO9MO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qO9MO.png" alt="blergh" /></a></p>
  <h1>Another update</h1>
  <p>After Taplar pointed out how to call the method on the cast version the errors went away on the back door which is fantastic!</p>
  <p>The next wall I smashed into was that now for some reason the test can't see my mock anymore which is weird because the variable is local to the test.</p>
  <p><a href="https://i.stack.imgur.com/qfUwB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qfUwB.png" alt="closer" /></a></p>