This is my method.
redirect(){
const url = 'localhost:4100';
this.document.defaultView?.open(url, '_blank');
}
Obviously open() is present in defaultView. However, when I try to mock it, I'm getting an error open is not part of this object.
const spyOpen = spyOn(document.defaultView, 'open').and.callFake(
(url: string, target: string = '_blank'): void => {
url;
target;
}
);
In Jest, it can be:
The problem is that if
document.defaultView.openis really not a function at runtime, Jest will fail to spy on it, in this case it cannot make use of spies maintained by Jest and needs to be cleaned up manually:The same can be applied to Jasmine, adjusted for difference in API.