I have a following problem. I have a singleton class. There is a getInstance static method which call constructor. singleton.ts:
class Singleton {
private static singletonInstance: Singleton;
private constructor(){
...doSomething;
}
public static getInstance(): Singleton {
if(!Singleton.singletonInstance){
Singleton.singletonInstance = new Singleton();
}
throw new Error('stupid error');
}
}
singleton.spec.ts:
it('getInstance should throw an error', () => {
expect(Singleton.getInstance()).toThrow(new Error('stupid error'));
})
'getInstance should throw an error' is failing, because... getInstance() didn't throw the error.
On console output I can notice the error was throw - it prints 'stupid error'.