2

Jest not implemented for window.open, getting error on the console but unit test is getting pass with console error

a.ts file

myFun(){
 window.open('https://stackoverflow.com/', '_blank');
}

a.spec.ts

providers: [
      {
        useValue: { open: jest.fn() }
      }
    ]
----------------------------------------------
it('should open window on submit ', () => {
      const spy = spyOn(window, 'open').and.callFake(() => {});
      component.submit();
      expect(spy).toHaveBeenCalledWith('https://stackoverflow.com/', '_blank');
    });

Console error

`console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Not implemented: window.open
        at module.exports (C:\workspace\guide-ast\fis-my-support\node_modules\jsdom\lib\jsdom\browser\not-implemented.js:9:17)
        at C:\workspace\guide-ast\fis-my-support\node_modules\jsdom\lib\jsdom\browser\Window.js:594:7
        at SafeSubscriber._next (C:\workspace\guide-ast\fis-my-support\apps\ui\src\app\ticket-creation\formly-ticket-page\formly-ticket-page.component.ts:167:18)
        at SafeSubscriber.Object.<anonymous>.SafeSubscriber.__tryOrUnsub (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\Subscriber.ts:265:10)
        at SafeSubscriber.Object.<anonymous>.SafeSubscriber.next (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\Subscriber.ts:207:14)
        at Subscriber.Object.<anonymous>.Subscriber._next (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\Subscriber.ts:139:22)
        at Subscriber.Object.<anonymous>.Subscriber.next (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\Subscriber.ts:99:12)
        at TakeWhileSubscriber.Object.<anonymous>.TakeWhileSubscriber.nextOrComplete (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\operators\takeWhile.ts:102:19)
        at TakeWhileSubscriber.Object.<anonymous>.TakeWhileSubscriber._next (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\operators\takeWhile.ts:96:10)
        at TakeWhileSubscriber.Object.<anonymous>.Subscriber.next (C:\workspace\guide-ast\fis-my-support\node_modules\rxjs\src\internal\Subscriber.ts:99:12) undefined`
Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
Rijo
  • 2,963
  • 5
  • 35
  • 62
  • 2
    Does this answer your question? [How to mock the JavaScript window object using Jest?](https://stackoverflow.com/questions/41885841/how-to-mock-the-javascript-window-object-using-jest) – Mike Doe May 22 '20 at 08:14
  • I tried this scenario but not working – Rijo May 22 '20 at 08:21

0 Answers0