testing-library / testing-library/user-event

`setup({ writeToClipboard: true})` does not write to clipboard

Open
#1,289 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs assessment
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
  1. Run userEvent.setup({ writeToClipboard: true }) at the start of a script
  2. Call navigator.clipboard.writeText("Hello")
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.