coder icon indicating copy to clipboard operation
coder copied to clipboard

Test flake: Tests for `useClipboard` don't consistently flush state changes

Open buenos-nachos opened this issue 1 year ago • 0 comments

Issue

  • Certain assertions for test cases in useClipboard don't consistently succeed. Previous failures:
    • https://github.com/coder/coder/actions/runs/9033841899/job/24825045193
    • https://github.com/coder/coder/actions/runs/8695401295/job/23846383155#step:4:17
  • The test file in question

Notes

  • Inclined to believe that the issue is with the test, rather than the functionality, because while the test is new, the functionality has mostly been unchanged since it was first created over a year ago
  • Testing clipboard functionality is weird in general, so depending on what's found after digging, it might be better to make some E2E tests instead
    • Though obviously it'd be better to have a single test for the clipboard, rather than needing to have ad-hoc tests for everything that just happens to use the clipboard
    • The main issue is that React Testing Library will inject a clipboard mock the moment you try to set up any kind of user session with user.setup. This is great most of the time, but the problem is that the clipboard always succeeds, and there's no way to remove the mock. Once there's a single setup, the entire test is basically "tainted" with false positives
    • Because the tests actually need to verify that the fallback functionality works when a clipboard isn't available on HTTP-only connection, we can't use user.setup at all

buenos-nachos avatar May 10 '24 19:05 buenos-nachos