Typescript 如何模拟Singleton';中使用的外部lib;谁的构造函数?打字和笑话

Typescript 如何模拟Singleton';中使用的外部lib;谁的构造函数?打字和笑话,typescript,testing,mocking,jestjs,singleton,Typescript,Testing,Mocking,Jestjs,Singleton,我必须测试一个单例,但在他的构造函数中我调用了一个外部方法。我怎么能嘲笑它 import externalLib from 'externalModule'; class MySingleton { public static _instance: MySingleton; private constructor { externalLib.method() // I have to mock externalLib } public static getInst

我必须测试一个单例,但在他的构造函数中我调用了一个外部方法。我怎么能嘲笑它

import externalLib from 'externalModule';

class MySingleton {

  public static _instance: MySingleton;

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

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

    return new MySingleton();
  }

}

export default MySingleton.getInstance();

谢谢。

你可以这样做(doc)

import extLib from 'extLib';

jest.mock('extLib');

test('should fetch users', () => {
  extLib.method.mockResolvedValue(MyReturnValue);

  // or you could use the following depending on your use case:
  // extLib.method.mockImplementation(() => Promise.resolve(MyReturnValue))

  // Your testing code here...
});