testing-library / testing-library/user-event

Clipboard stub state is not reset in Vitest (or any runner without globals)

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

Nobody has claimed this yet.

enhancement environment
Dominant language
TypeScript
Stars
2.3k
Forks
258
PR merge metrics
No merged PRs in 30d

Description

Reproduction example

https://codesandbox.io/p/github/mcous/user-event-clipboard-leak/main?import=true

Prerequisites
  1. Configure Vitest in non-globals mode (the default)
  2. Call userEvent.setup() in a test
  3. Copy something to the clipboard in a test
  4. Inspect the clipboard in a later test
Expected behavior

Clipboard state should be reset between tests, or there should be a documented way to add that clipboard cleanup to one's suite.

Without globals mode, I see a couple possible solutions. Adding one or both of these would be appropriate, I think:

  1. The module should export cleanup functions from the root (like other @testing-library modules) so a user can add cleanup to their test hooks manually
  2. Calling userEvent.setup should reset clipboard stub state
Actual behavior
  • Clipboard stub state is retained between tests
User-event version

14.5.2

Environment

No response

Additional context

Let me know if you need any assistance resolving this bug! I maintain @testing-library/svelte and I'm happy to jump in here, too, if it's helpful

As a workaround - since I'm not willing to enable globals mode - I am currently adding this to my global test setup file. It's not pretty, obvious, nor documented, so it would be nice if user-event could provide a blessed way to do this in Vitest

import { afterAll, afterEach } from 'vitest';
import {
  detachClipboardStubFromView,
  resetClipboardStubOnView,
} from '@testing-library/user-event/dist/esm/utils/dataTransfer/Clipboard.js';

afterEach(() => {
  resetClipboardStubOnView(window);
});

afterAll(() => {
  detachClipboardStubFromView(window);
});

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 clipboard utilities at dist/esm/utils/dataTransfer/Clipboard.js and trace how userEvent.setup() manages the stub. Review the root exports and the Vitest non-globals reproduction to decide how cleanup should be exposed or triggered. Done means clipboard state no longer leaks between tests, with the documented cleanup path covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
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.