Skip to content

Conversation

@kylecarbs
Copy link
Member

Problem

The EscapeKeyCloses and OverlayClickCloses story tests were failing in Chromatic with this error:

expect(element).not.toBeInTheDocument()
expected document not to contain element, found <div role="dialog">

The tests were using fixed 100ms timeouts after user interactions, which wasn't reliable in the Chromatic rendering environment. React state updates are asynchronous and timing can vary across environments.

Solution

Replace fixed timeouts with waitFor() from @storybook/test, which polls until the condition is met or times out. Also add a small delay before triggering user actions to ensure the Modal's useEffect has attached event listeners.

Changes

  • EscapeKeyCloses test: Add setup delay + use waitFor() to wait for modal removal
  • OverlayClickCloses test: Add setup delay + use waitFor() to wait for modal removal
  • Code quality: Replace let with const for modal queries and use descriptive variable names
  • Consistency: Improve comments in ContentClickDoesNotClose and LoadingPreventsClose tests

This makes the tests more reliable across different rendering environments including CI, local development, and Chromatic.

Generated with cmux

Replace fixed 100ms timeouts with waitFor() for reliable async testing.
The Escape key test was failing in Chromatic because state updates
weren't completing within the fixed timeout window.

Changes:
- Use waitFor() for EscapeKeyCloses and OverlayClickCloses tests
- Add delay before user actions to ensure event listeners are attached
- Replace 'let' with 'const' for modal queries
- Improve code clarity with descriptive variable names
@kylecarbs kylecarbs merged commit 64aa8e9 into main Oct 23, 2025
9 of 12 checks passed
@kylecarbs kylecarbs deleted the fix-modal-chromatic-main branch October 23, 2025 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant