Simplified problem case:
export class MyClass {
constructor() {
this.myMethod();
}
myMethod() {
console.log(42);
}
}
Testing the constructor:
describe('CLASS: MyClass', () => {
let sut: MyClass;
beforeEach(() => {
jest.clearAllMocks();
sut = new MyClass();
});
describe('CONSTRUCTOR', () => {
test('should construct correctly and call myMethod', () => {
const spy = jest.spyOn(sut, 'myMethod').mockImplementationOnce(jest.fn());
expect(sut).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
});
});
Of course this doesn't work, as the spy is initiated after sut is constructed, so it can't register the call.
Neither is it possible to initiate the spy before sut, as it can't spy on something that doesn't exist yet.
Nor did I have success trying to spy on MyClass.prototype.
Sure, I could spy on the implementation details of myMethod (basically jest.spyOn(console, 'log'). But that defies the separation of units for testing.
It's probably trivial, but what am I missing, how to get this very simple test to work?