react-testing
React Testing Library + Vitest/Jest best practices (2025–2026): unit, integration, mocking, async, CI/CD, and advanced (visual-regression, a11y, performance) testing. Covers the Testing Trophy philosophy, query priority (getByRole first, getByTestId last), userEvent over fireEvent, findBy/waitFor async patterns, custom render with providers, vi.fn/spyOn/mock, renderHook, React Query + Suspense testing, Vitest vs Jest config (pools, sharding, coverage thresholds), and when to reach for axe-core, Chromatic/Playwright CT, or the Profiler. Use when writing or reviewing React component/hook tests, picking a query or interaction API, setting up Vitest/Jest config or coverage gates, or deciding on visual/a11y/perf testing. Do NOT use for non-React testing or E2E flow design unrelated to components.
- License 许可
- MIT
- 所属仓库
- fagerbergj/quack
- 最近更新
- 2026-11-15
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- react-testing
- license
- MIT
- description
- React Testing Library + Vitest/Jest best practices (2025–2026): unit, integration, mocking, async, CI/CD, and advanced (visual-regression, a11y, performance) testing. Covers the Testing Trophy philosophy, query priority (getByRole first, getByTestId last), userEvent over fireEvent, findBy/waitFor async patterns, custom render with providers, vi.fn/spyOn/mock, renderHook, React Query + Suspense testing, Vitest vs Jest config (pools, sharding, coverage thresholds), and when to reach for axe-core, Chromatic/Playwright CT, or the Profiler. Use when writing or reviewing React component/hook tests, picking a query or interaction API, setting up Vitest/Jest config or coverage gates, or deciding on visual/a11y/perf testing. Do NOT use for non-React testing or E2E flow design unrelated to components.
02 资源文件 3
- 参考文档references/advanced.md3.8 KB
- 参考文档references/ci-and-tooling.md2.7 KB
- 参考文档references/recipes.md4.8 KB
03 SKILL.md 正文
React testing (RTL + Vitest/Jest)
Overview
Decision-level guidance for testing React the way users actually use it. The guiding rule: the more your tests resemble how the software is used, the more confidence they give (Kent C. Dodds). Invest most in integration-level tests; test behaviour, not internals.
Code templates and the full tables live in references/recipes.md. Config, parallelism, and coverage live in references/ci-and-tooling.md. Visual/a11y/perf live in references/advanced.md.
quack's web package (packages/web) uses Vitest + RTL + user-event - match that grain before adding deps.
When to use
- Writing or reviewing a component, hook, form, or async-fetch test.
- Picking a query (
getByRolevsgetByTestId) or interaction API (userEventvsfireEvent). - Setting up / tuning Vitest or Jest config, parallelism, coverage thresholds, or CI gates.
- Deciding whether to add visual-regression, accessibility, or performance testing.
When NOT to use
- Non-React code or pure E2E user-journey design.
- A question already answered by the repo's existing test setup - read those first.
Core rules (the reflexes)
- Never test internal state or instance methods. Test what the user sees.
- Query by accessibility, not implementation. Role/label/text - never CSS class or component name.
- Don't wrap
render()inact()- RTL already does. Don't keep awrappervar; destructure{ rerender }. - Use the global
screenfor queries (pre-bound todocument.body). - Default to
userEvent, notfireEvent- it models the real browser event chain and is async (awaitit). - Async =
findBy*/waitFor/waitForElementToBeRemoved. NeversetTimeoutor fake timers for async UI.
Query priority (memorize this order)
getByRole → getByLabelText → getByPlaceholderText → getByText → getByDisplayValue → getByAltText → getByTestId (last resort). Variant by intent:
getBy*- element you expect present (throws if missing = useful signal).queryBy*- asserting absence (returnsnull).findBy*- element appearing after async work (polls viawaitFor, ~1000ms default).
Mocking, at a glance
vi.fn()- a fake function (assert calls/args).vi.spyOn(obj, 'm')- observe/replace one method.vi.mock('mod', factory)- replace a whole module (API client, custom hook). Jest mirrors all three.- ESM mock hoisting gotcha →
vi.hoisted(). Network-level mocking → MSW, not per-call stubs. - Hooks →
renderHook(built into@testing-library/reacton React 18+); passwrapperfor providers.
Tooling decisions
- New Vite/ESM project → Vitest (native ESM, shares
vite.config.ts, faster watch). React Native / large legacy Jest → stay on Jest. Seereferences/ci-and-tooling.md. - Coverage gate:
thresholdsin config fails CI on drop. Industry baseline ~80% statements/ functions/lines, ~75% branches. Reportlcovto Codecov/Coveralls. - Snapshots: avoid for app components (rubber-stamped, break on irrelevant changes, test structure not behaviour). OK only for truly static UI-library primitives.
Advanced (reach for these deliberately - references/advanced.md)
- Visual regression: Chromatic (Storybook-native, component-level) → first choice if Storybook exists; Playwright CT (real browser, free, manual baselines); Percy/Applitools (page-level).
- Accessibility:
jest-axeinside the RTL suite; runaxe()after interactions, not just on mount. Catches missing labels/contrast/ARIA - not whether alt text is meaningful. - Performance: React
<Profiler>API;vitest-react-profilerfor render-count assertions; performance budgets (unit render-time → page TTI → Lighthouse CI) in the pipeline.