Comfy-Org / Comfy-Org/ComfyUI_frontend
test: restore navigator.clipboard in afterEach to prevent global-state leakage
- Dominant language
- TypeScript
- Stars
- 2k
- Forks
- 699
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 490
Description
## Summary
In `src/composables/graph/useImageMenuOptions.test.ts`, the `mockClipboard` helper overrides `navigator.clipboard` via `Object.defineProperty` but it is never restored in the `afterEach` teardown. This can leak clipboard state into unrelated tests and cause order-dependent failures.
The same pattern exists in other test files that mock clipboard/console behavior, so a consistent fix should be applied across affected tests.
## Suggested Fix
Capture the original `navigator.clipboard` descriptor before tests run and restore it in `afterEach`:
```ts
const originalClipboardDescriptor = Object.getOwnPropertyDescriptor(navigator, 'clipboard')
afterEach(() => {
vi.restoreAllMocks()
if (originalClipboardDescriptor) {
Object.defineProperty(navigator, 'clipboard', originalClipboardDescriptor)
}
})
```
Alternatively, use Vitest's built-in `vi.stubGlobal` / `vi.unstubAllGlobals` utilities for cleaner per-test isolation, in line with the project's Vitest mocking guidelines.
## References
- Flagged in PR #10021 (comment: https://github.com/Comfy-Org/ComfyUI_frontend/pull/10021#discussion_r2940178159)
- Requested by @christian-byrne as a followup cleanup task
## Affected Files
- `src/composables/graph/useImageMenuOptions.test.ts`
- Potentially other test files following the same clipboard/console mock-without-restore pattern
┆Issue is synchronized with this [Notion page](https://www.notion.so/Issue-10049-test-restore-navigator-clipboard-in-afterEach-to-prevent-global-state-leakage-3256d73d36508118b077f9d1a68c7170) by [Unito](https://www.unito.io)
Contributor guide
Assessment
This issue has not been assessed yet.