ui-testing
Resilient React component testing strategies focusing on user behavior. Use when writing or reviewing component-level React tests, fixing flaky component tests, or establishing React testing patterns. DO NOT USE FOR: Playwright E2E tests (use webapp-testing), canvas game interaction (use browser-canvas-testing), TDD workflow (use test-driven-development), debugging failures (use systematic-debugging), UI component design (use frontend-design), or randomized property tests (use property-based-testing).
- License 许可
- MIT
- 最近更新
- 2026-08-16
- Stars
- ★ 3
- Forks
- 0
01 前言 Front Matter
- name
- ui-testing
- description
- Resilient React component testing strategies focusing on user behavior. Use when writing or reviewing component-level React tests, fixing flaky component tests, or establishing React testing patterns. DO NOT USE FOR: Playwright E2E tests (use webapp-testing), canvas game interaction (use browser-canvas-testing), TDD workflow (use test-driven-development), debugging failures (use systematic-debugging), UI component design (use frontend-design), or randomized property tests (use property-based-testing).
02 资源文件 1
- 参考文档testing-patterns.md6.8 KB
03 SKILL.md 正文
UI Testing Skill
Guide for creating resilient, maintainable UI component tests.
When to Use
- Writing tests for React components
- Debugging flaky or brittle tests
- Reviewing test code
- Establishing testing patterns for a project
- Refactoring tests after UI changes
Core Philosophy
"Test behavior, not implementation."
Users don't care about internal state or DOM structure—they care about what they can see and do.
Testing Priorities (Confidence vs. Cost)
High Confidence, Low Cost → Prioritize
├── User interactions (clicks, typing)
├── Visible content changes
├── Accessibility requirements
└── Error states
Medium Confidence, Medium Cost → Include
├── Integration with data fetching
├── Complex state transitions
└── Edge cases
Low Confidence, High Cost → Minimize
├── Implementation details
├── Internal state values
└── CSS/styling
Selector Strategy (Priority Order)
1. Accessible Queries (Preferred)
// Best: How users and assistive tech find elements
getByRole("button", { name: "Submit" });
getByLabelText("Email address");
getByPlaceholderText("Search...");
getByText("Welcome back");
getByAltText("User avatar");
2. Semantic Queries (Acceptable)
// Good: Semantic HTML attributes
getByTitle("Close dialog");
getByDisplayValue("current input value");
3. Test IDs (Last Resort)
// Fallback: When no accessible option exists
getByTestId("complex-data-grid");
Never Use
// Fragile: Breaks on any refactor
container.querySelector(".btn-primary");
wrapper.find("div > span:first-child");
getByClassName("header-title");
Test Structure Pattern
describe("ComponentName", () => {
// Group by user goal, not by method
describe("when user [does action]", () => {
it("should [expected outcome visible to user]", () => {
// Arrange: Set up component state
render(<Component {...props} />);
// Act: Simulate user behavior
await userEvent.click(getByRole("button", { name: "Submit" }));
// Assert: Check visible outcomes
expect(getByText("Success!")).toBeInTheDocument();
});
});
});
Common Patterns
Testing User Input
it("should update display when user types", async () => {
render(<SearchBox />);
const input = getByRole("searchbox");
await userEvent.type(input, "query");
expect(input).toHaveValue("query");
});
Testing Async Operations
it("should show results after search", async () => {
render(<SearchResults />);
await userEvent.click(getByRole("button", { name: "Search" }));
// Wait for visible change, not implementation detail
expect(await findByText("3 results found")).toBeInTheDocument();
});
Testing Accessibility
it("should be accessible", async () => {
const { container } = render(<Component />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
See testing-patterns.md for more detailed patterns.
Project Configuration
[CUSTOMIZE] Add your project's testing setup:
// Test utilities location: [path]
// Custom render with providers: [path]
// Mock patterns: [path]
// Test data factories: [path]
Debugging Flaky Tests
- Identify the flake: Run test in isolation 10+ times
- Check async handling: Are we waiting for the right thing?
- Check test isolation: Does order matter? Shared state?
- Check timing: Race conditions in component or test?
- Add debugging:
screen.debug(),logRoles(container)
Gotchas
| Trigger | Gotcha | Fix |
|---|---|---|
Asserting on component.state or internal refs directly |
Breaks on internal refactoring even when user-visible behavior is unchanged | Assert on rendered text, visible elements, or dispatched events — not internal component state |
Adding expect(tree).toMatchSnapshot() to every component test |
One-character internal change triggers cascading snapshot failures; reviewers approve noise without reading | Replace with targeted assertions on specific text, attributes, or DOM structure |
Setting waitFor timeout > 1000ms to make a flaky test pass |
Masks real performance or async timing issues; adds hard delay to the entire suite | Diagnose why the element isn't ready sooner; fix the underlying async handling |
| Writing assertions that validate a third-party library's own behavior (e.g., checking React Query fetched) | Tests validate the library, not your code; break on library version changes | Trust the library; test your code's response to its outputs — not the library's own logic |
Suppressing or ignoring act() warnings in the console |
Async state update issues are hidden; test results become unreliable | Fix the timing: wrap state updates in act() or switch from fireEvent to userEvent |
Using data-testid when getByRole would work |
Tests diverge from user accessibility model; role-based queries are more resilient | Follow locator priority: getByRole > getByLabel > getByTestId > CSS (last resort) |
| Asserting on internal library DOM nodes (e.g., react-select internals) | Tests break on library upgrade, not on your code breaking | Trust the dependency; test what the user sees and can interact with |
04 同仓库更多 Skills
webapp-testing
Playwright end-to-end testing guidance for web apps, with practical patterns and setup steps. Use when creating or improving browser-based E2E coverage, test stability, and CI execution. DO NOT USE FOR: React component tests (use ui-testing), canvas game interaction (use browser-canvas-testing), TDD workflow (use test-driven-development), debugging failures (use systematic-debugging), or randomized property tests (use property-based-testing).
verification-before-completion
Evidence-based verification checklist before marking work complete. Use before PRs, releases, marking tickets done, or any "I'm finished" declaration; also when naming a baseline commit for a differential suite claim, when designing a mutation campaign or red-run evidence, when making an absence claim, a universal claim, or a one-time demonstration offered as a standing guarantee, when attributing a measurement or claiming a sweep or verification loop is complete, and when stating what a check or a CI suite will do about a change. DO NOT USE FOR: post-merge cleanup or archival (use post-pr-review) or processing GitHub review comments (use code-review-intake).
validation-methodology
Reusable validation and review methodology for staged validation, failure triage, and prosecution-depth setup. Use when running validation ladders, triaging failures, or executing adversarial review passes. DO NOT USE FOR: CE Gate orchestration, specialist dispatch ownership, or step execution flow (keep those in Code-Conductor).
upstream-onboarding
Shared opening-phase protocol for upstream agents (Experience-Owner, Solution-Designer, Issue-Planner) and Code-Conductor when invoked on an existing GitHub issue. Renders a scaled context brief and runs a standards check on inherited work at each phase boundary. Use when a user-invocable agent receives a request referencing an existing GitHub issue and must load solution-authoring and upstream-onboarding at the opening phase. DO NOT USE FOR: subagent dispatches (which already operate within an assessed session context); post-merge review (use post-pr-review); research or non-tree subagents.
ui-iteration
Reusable screenshot-driven UI polish workflow for iterative visual refinement. Use when improving layout, hierarchy, spacing, feedback, or Tailwind-based presentation through repeated review-and-adjust passes. DO NOT USE FOR: functional bug fixing, accessibility-only audits, or major redesign strategy work (use Code-Smith or frontend-design).
tracking-format
Tracking-file frontmatter and coordination format guidance. Use when creating or reviewing local tracking files and related handoff conventions. DO NOT USE FOR: session-memory plan authoring rules outside the documented references here.