2

My team runs a website created with React. For various reasons, I need to use the deprecated document.execCommand('copy') method of copying to the clipboard. When writing jest unit tests, I am mocking execCommand and making sure it was called with expect(document.execCommand).toHaveBeenCalledWith('copy');

I want to test the actual contents of the clipboard after copying. Is there a way to test for this using jest?

Justin
  • 154
  • 1
  • 17

1 Answers1

1

No.

Quoting from a previous answer:

Jest tests are running in the JSdom environment and not all of the properties are defined, but you should be able to define the function before spying.

I would recommend mocking the Clipboard API, as that is a more modern way of reading and writting to the user's clipboard.

(Unless, your test is very simple. In which case I'd say it's not worth the trouble)

Edit: There's some discussion on how to do this in an open issue on jsdom's repository

Joaquim Esteves
  • 400
  • 2
  • 12