title: How to mock navigator.clipboard.writeText() in Jest?
body: <p>I have tried the following 4 options after looking at Jest issues and <a href="https://stackoverflow.com/questions/59640985/how-to-mock-navigator-clipboard-writetext-in-vue-test-utils">SO answers</a>, but I am either getting TypeScript errors or runtime errors. I would really like to get option 1 (spyOn) working.</p>
  
  <pre><code>// ------ option 1 -----
  // Gives this runtime error: "Cannot spyOn on a primitive value; undefined given"
  const writeText = jest.spyOn(navigator.clipboard, 'writeText');
  
  // ------ option 2 -----
  Object.defineProperty(navigator, 'clipboard', {
      writeText: jest.fn(),
  });
  
  // ------ option 3 -----
  // This is from SO answer but gives a TypeScript error
  window.__defineGetter__('navigator', function() {
      return {
          clipboard: {
              writeText: jest.fn(x =&gt; x)
          }
      }
  })
  
  // ------ option 4 -----
  const mockClipboard = {
      writeText: jest.fn()
  };
  global.navigator.clipboard = mockClipboard;
  </code></pre>