mardi 21 janvier 2020

How can i mock external lib who is used in Singleton's constructor ? Typescript and Jest

I have to test a Singleton but in his constructor i called a external method. How can i mock it ?

import externalLib from 'externalModule';

class MySingleton {

  public _instance: MySingleton;

  private constructor {
    externalLib.method() // I have to mock externalLib
  }

  public getInstance() {
    if (_instance) {
      return _instance;
    }

    return new MySingleton();
  }

}

export default MySingleton.getInstance();

Thank you.

Aucun commentaire:

Enregistrer un commentaire