title: How can I mock the JavaScript 'window' object using Jest?
body: <p>I need to test a function which opens a new tab in the browser</p>
  <pre><code>openStatementsReport(contactIds) {
    window.open(`a_url_${contactIds}`);
  }
  </code></pre>
  <p>I would like to mock <em>window</em>'s <code>open</code> function, so I can verify the correct URL is passed in to the <code>open</code> function.</p>
  <p>Using Jest, I don't know how to mock <code>window</code>. I tried to set <code>window.open</code> with a mock function, but this way doesn't work. Below is the test case:</p>
  <pre><code>it('the correct URL is called', () =&gt; {
    window.open = jest.fn();
    statementService.openStatementsReport(111);
    expect(window.open).toBeCalled();
  });
  </code></pre>
  <p>But it gives me the error</p>
  <pre class="lang-none prettyprint-override"><code>expect(jest.fn())[.not].toBeCalled()
  
  jest.fn() value must be a mock function or spy.
      Received:
        function: [Function anonymous]
  </code></pre>
  <p>What should I do to the test case?</p>