testing-library / testing-library/user-event
`setup({ writeToClipboard: true})` does not write to clipboard
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.3k
- Forks
- 258
- PR merge metrics
- No merged PRs in 30d
Description
Reproduction example
https://codesandbox.io/p/sandbox/modest-hermann-pp9wgz
Prerequisites
- Run
userEvent.setup({ writeToClipboard: true })at the start of a script - Call
navigator.clipboard.writeText("Hello") - Confirm the text "Hello" has not been copied to the actual clipboard
Expected behavior
When passing writeToClipboard: true I expect the clipboard stub to copy text to the actual clipboard as normal. Currently it doesn't actually make a difference whether writeToClipboard is set or not. The property name seems misleading.
This is a problem in Storybook where we instrument user's stories/components with userEvent.setup(), causing their clipboard behavior to no longer work.
We can work around it by restoring the original clipboard after calling setup() but that's potentially brittle and does not allow users to leverage the actual stub in their tests. I have attempted to use detachClipboardStub but that method does not appear to be publicly accessible.
Ideally we would have a clipboard stub that actually calls the real clipboard as well, if possible/available.
Actual behavior
Calling navigator.clipboard.writeText("hello") is intercepted by user-event's clipboard stub and doesn't forward the call to the actual clipboard API, regardless of the writeToClipboard option.
User-event version
14.6.1
Environment
Testing Library framework:
JS framework:
Test environment:
DOM implementation:
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the CodeSandbox reproduction and trace the clipboard stub installed by userEvent.setup({ writeToClipboard: true }). Check how navigator.clipboard.writeText is intercepted, then verify that the true option forwards to the actual clipboard when available while preserving the existing stub behavior otherwise.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100