Exposing Private Methods for Test

In most cases, you shouldn't expose or test private methods. If you have to stub private methods, see Exposing Private Methods for Test. But in rare cases, you may have to expose private methods for testing. To do so, you can add the method __DO_NOT_USE_IN_PROD_exposePropsForTesting to the class. This method should export only the private methods and properties that are necessary for the test

  // eslint-disable-next-line camelcase
  __DO_NOT_USE_IN_PROD_exposePropsForTesting() {
    return {
      onFirstOpen: this.onFirstOpen.bind(this),
    };
  }

  • NOTE: you'll need to add the eslint disablement as well as the .bind(this) for this to work.

You can see an example of this here


Backlinks