3

I am using vue test utils - Jest framework for unit testing of my vue file. I'm not able to test the below line.

const inBrowser = typeof window !== 'undefined';

My question is how to set JavaScript Window as undefined. Is there any way to set window as undefined Browser Object Model (BOM)?

Ahmad Shahwan
  • 1,662
  • 18
  • 29
  • 1
    Does this answer your question? [How can I set window to undefined in order to test the SSR rendering in an isomorphic application?](https://stackoverflow.com/questions/54038514/how-can-i-set-window-to-undefined-in-order-to-test-the-ssr-rendering-in-an-isomo) – Ahmad Shahwan Feb 20 '20 at 14:13

1 Answers1

-1

Based on this and this you can use the .spyOn function and set the window as undefined:

let windowSpy;

beforeEach(() => {
  windowSpy = jest.spyOn(global, 'window', 'get');
  windowSpy.mockImplementation(() => undefined);
});

it('window should be undefined.', () => {
  expect(window).toBeUndefined();
});
Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130