Skip to content

docs: prefer userEvent in unit test examples#34632

Merged
kylegach merged 1 commit into
storybookjs:nextfrom
rohanadepu:docs/unit-test-user-event-guidance
May 1, 2026
Merged

docs: prefer userEvent in unit test examples#34632
kylegach merged 1 commit into
storybookjs:nextfrom
rohanadepu:docs/unit-test-user-event-guidance

Conversation

@rohanadepu
Copy link
Copy Markdown
Contributor

@rohanadepu rohanadepu commented Apr 26, 2026

What

Update the stories-in-unit-tests examples to prefer userEvent.setup() and await user.click(...) over fireEvent.click(...).

Why

Testing Library recommends userEvent for interactions that model user behavior. This also makes the unit-test examples more consistent with Storybook interaction testing, where userEvent is provided to play functions.

How

  • Replaced fireEvent imports/usages in the unit-test snippets with @testing-library/user-event
  • Kept screen queries in the unit-test examples because composed stories run inside the external test renderer
  • Added a short note explaining when to use screen vs the canvas queries provided to play functions

Issue

Fixes #31868

Validation

  • corepack yarn docs:check
  • corepack yarn fmt:check docs/_snippets/component-test-with-testing-library.md docs/_snippets/single-story-test.md docs/_snippets/multiple-stories-test.md docs/writing-tests/integrations/stories-in-unit-tests.mdx

Summary by CodeRabbit

  • Documentation
    • Updated testing documentation snippets to replace fireEvent.click() with @testing-library/user-event for improved interaction testing.
    • Added clarification on which Testing Library query APIs to use when testing stories in unit tests, with guidance on using screen for provided examples and canvas queries within story play functions.

@rohanadepu rohanadepu marked this pull request as ready for review April 26, 2026 01:34
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6077bb47-8946-4902-a0ce-a7d710995813

📥 Commits

Reviewing files that changed from the base of the PR and between b9549a6 and 85b0a07.

📒 Files selected for processing (4)
  • docs/_snippets/component-test-with-testing-library.md
  • docs/_snippets/multiple-stories-test.md
  • docs/_snippets/single-story-test.md
  • docs/writing-tests/integrations/stories-in-unit-tests.mdx

📝 Walkthrough

Walkthrough

Documentation updates replace fireEvent.click() with @testing-library/user-event interactions across multiple testing snippet examples. Files import userEvent, initialize via setup(), and use async await user.click() patterns. Documentation clarification added for Testing Library query API usage in unit tests.

Changes

Cohort / File(s) Summary
Test Snippet Updates
docs/_snippets/component-test-with-testing-library.md, docs/_snippets/multiple-stories-test.md, docs/_snippets/single-story-test.md
Replaces fireEvent.click() with await user.click() across all framework variants (React/Vue, JS/TS). Removes fireEvent from imports, adds userEvent imports and userEvent.setup() initialization in test blocks.
Testing Documentation
docs/writing-tests/integrations/stories-in-unit-tests.mdx
Adds clarification on Testing Library query API usage: recommends screen queries for unit tests and canvas queries within story play functions for proper scope.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@valentinpalkovic
Copy link
Copy Markdown
Contributor

Hi @rohanadepu,

Due to a recent high volume of unreviewed AI-generated PRs, we are requesting verification and proof that the implemented fix actually works. Please provide a simple GIF/Video or image of how the fix works, optimally with before-and-after comparisons.

Thank you for your understanding!

@rohanadepu
Copy link
Copy Markdown
Contributor Author

Hi @valentinpalkovic, absolutely. Thanks for explaining.

Here is a quick VS Code capture showing the before/after diff for the affected docs snippet:

VS Code before/after diff

This PR is docs-only. The relevant change is replacing the fireEvent.click(...) examples with userEvent.setup() + await user.click(...), and adding the clarification that external unit tests can use Testing Library screen queries while story play functions should prefer scoped canvas queries.

I also verified locally with:

  • corepack yarn docs:check
  • corepack yarn fmt:check docs/_snippets/component-test-with-testing-library.md docs/_snippets/single-story-test.md docs/_snippets/multiple-stories-test.md docs/writing-tests/integrations/stories-in-unit-tests.mdx
  • git diff --check

Copy link
Copy Markdown
Contributor

@kylegach kylegach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @rohanadepu! Appreciate it. 🙂

@kylegach kylegach merged commit f9060db into storybookjs:next May 1, 2026
5 of 7 checks passed
@github-project-automation github-project-automation Bot moved this from Human verification to Done in Core Team Projects May 1, 2026
@github-actions github-actions Bot mentioned this pull request May 4, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Documentation]: Inconsistency Interaction vs Unit

3 participants