react-testing-architect
Architect-level React test strategy — deciding what belongs in type checks, unit, component, integration and end-to-end tests, where the network boundary is mocked, how fixtures and factories are owned, and which flake and runtime budgets the suite must respect. Use when designing a test suite or when tests are slow, brittle, or passing while the app is broken.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- react-testing-architect
- license
- MIT
- metadata
- { "crusor-path": "website-architect/frontend-development-architect/react-architect/react-testing-architect", "crusor-domain": "website-architect", "crusor-origin": "original", "crusor-sources": "testing-library.com/docs/queries/about; playwright.dev/best-practices; vercel-labs/agent-skills@063bee94 (react-best-practices)", "crusor-triggers": "testing, vitest, jest, testing-library, playwright, msw, e2e, coverage, flake", "crusor-verified-on": "2026-10-30" }
- description
- Architect-level React test strategy — deciding what belongs in type checks, unit, component, integration and end-to-end tests, where the network boundary is mocked, how fixtures and factories are owned, and which flake and runtime budgets the suite must respect. Use when designing a test suite or when tests are slow, brittle, or passing while the app is broken.
- compatibility
- React 18+; Vitest or Jest, React Testing Library, Playwright
03 SKILL.md 正文
React Testing Architecture
A test suite is an architecture document. If the suite needs a rendered DOM to prove a business rule, the rule probably lives in the wrong layer.
When to use
- Starting a suite, or when coverage is high and production still breaks.
- When tests are rewritten every time a component's internals change.
- When deciding whether a behaviour needs RTL, an integration test, or E2E.
- When CI time or flake rate is the actual problem.
When NOT to use
- Fixing a single assertion in an already-sound suite.
- Pure styling work with no behaviour contract.
Decision 1 — The layers and their jobs
| Layer | Proves | Tool | Budget |
|---|---|---|---|
| Types | contracts between modules | tsc --noEmit |
seconds |
| Unit | pure logic: reducers, formatters, validators, selectors | Vitest | milliseconds |
| Component | a component's observable behaviour | RTL | tens of ms |
| Integration | a feature against a mocked network boundary | RTL + MSW | ~100 ms |
| E2E | the user journey through a real browser + real server | Playwright | seconds |
Rule of thumb: push every assertion down to the cheapest layer that can prove it. A validation rule tested through a browser is a suite that will be slow and still miss the rule.
Decision 2 — What belongs where
- Unit: business rules, state machines, money/date logic, permission matrices, parsers. No React imports.
- Component: rendering branches, disabled/loading/empty/error states, keyboard interaction, accessible name/role. Query by role and name.
- Integration: form → submit → request shape → success/error UI, with the network mocked at the boundary (MSW), not by mocking your own hooks.
- E2E: the three to ten journeys the product cannot survive without — sign in, primary task, checkout/publish, recovery from failure. Not every page.
Decision 3 — Rules
- Test behaviour, not implementation. No asserting on state variables, internal hook calls, class names, or component structure.
- Query like a user:
getByRole,getByLabelText,getByText.data-testidis the last resort for things with no accessible identity. - Mock the boundary, not the module graph. Mock HTTP (MSW) and timers; do not mock the component's own hooks — that tests the mock.
- One reason to fail. A test that fails for three possible causes will be weakened until it passes for all of them.
- Factories own fixtures. One builder per entity, with overrides; no 200-line literal repeated per file.
- No sleeps. Await a user-visible condition (
findBy*,expect.poll, Playwright auto-wait). AsetTimeoutin a test is a flake with a delay. - Deterministic time and randomness in unit/integration layers.
What not to test
- Third-party library behaviour.
- Implementation details that will be refactored (hook order, internal state).
- Snapshot blobs of whole pages: they bless whatever rendered, including bugs.
- The same rule at three layers "for safety" — that is triple maintenance, not triple confidence.
Flake and runtime budgets
| Budget | Target |
|---|---|
| Full unit+component suite | < 60 s locally |
| Integration suite | < 5 min |
| E2E critical path | < 10 min, parallelised |
| Flake rate | 0 tolerated; quarantine on first unexplained failure, with an owner and a date |
A quarantined test without an owner is a deleted test with extra steps.
Failure modes at scale
- Coverage theatre: 90% coverage of presentational components, 0% of the permission matrix.
- Mock drift: hand-written API mocks that no longer match the server, so integration tests pass against a fiction. Generate mocks from the same schema the server uses.
- Shared mutable fixtures producing order-dependent tests.
- E2E as the only layer: slow feedback, and failures with no localisable cause.
- Tests that assert the snapshot of a design system — every token change rewrites hundreds of files and hides real diffs.
Verification receipts
tsc --noEmitand lint are part of the same gate as tests.- The suite fails when the behaviour is deliberately broken (mutation check on the critical path).
- Timing report per layer, committed with the suite.
- Flake log: last N CI runs, failures by test id.
- Each critical journey has exactly one E2E owner and a named integration test beneath it.
04 同仓库更多 Skills
interaction-patterns-architect
Architect-level interaction design in code — choosing established patterns over invented ones, feedback and latency behaviour, destructive-action safeguards, navigation and overlay conventions, and the state model behind every interactive surface. Use when a screen has to behave, not just look.
design-system-ops-architect
Architect-level operation of a design system in code — token pipelines and versioning, component API and variant policy, the catalogue as the contract, adoption metrics, and the governance that stops a system from forking into three. Use when a component library must survive several teams.
design-handoff-architect
Architect-level design handoff — what must be specified before implementation starts, tokens over raw values, responsive and content-limit contracts, state matrices, and the review loop that catches drift between design and code. Use when a design is about to become code.
content-microcopy-architect
Architect-level UI content — where strings live, voice and terminology consistency, error and empty-state copy as part of the interface, translation-ready authoring, and the content model a product needs before it multiplies screens. Use when the words in the interface matter as much as the pixels.
ui-ux-implementation-architect
Architect-level direction for turning a design into working UI — the handoff contract, component ownership versus screen-level layout, interaction and state conventions, design QA gates, and the review habits that keep an implementation faithful without frozen pixels. Use before any specific UI implementation skill.
test-strategy-architect
Architect-level test strategy — allocating assertions across unit, integration, contract and end-to-end layers, designing for testability through dependency boundaries, fixture and data strategy, and the flake and coverage policies that keep a suite trustworthy. Use when defining how a codebase is tested.