React Testing
Assert what users can observe, using the test stack the project already owns.
When to use
Use for component, hook and form behavior tests. Use react-dev for implementation
and webapp-testing for real layout, navigation and complete browser flows.
Do not switch Jest to Vitest or install a second runner merely to use this guide.
Workflow
- Read the component's contract and repo test configuration. Identify the
existing runner, DOM environment, setup file, providers and network mocks.
Preserve project and coursework boundaries. Server components/framework-only
behavior may require the framework's integration or browser test support.
- Define observable cases: success, loading, empty, error and invalid input
where they apply. Write an independent expected result. For a bug, confirm
the regression test fails for the actual behavior before changing code.
- Render through real providers with a fresh cache/store per test. Use
getByRole with an accessible name or getByLabelText. Use a test ID only
when semantic queries cannot express the target.
- Create
userEvent.setup() per test and await interactions. Use findBy*
for arriving content, queryBy* for absence, and waitFor for asynchronous
assertions. Do not put repeated side-effectful clicks inside waitFor.
- Mock HTTP at the network boundary with the existing MSW setup when present;
fail unexpected requests, reset handlers between tests, and test error
responses. An injected service fake is also valid for a narrow component
contract; label it honestly as a fake, not a real integration test.
- Assert visible results and relevant external effects. Avoid checking private
state, render counts or calling internal handlers directly. Restore spies,
timers and global modifications after each test. Treat act warnings as work
to investigate, not noise to suppress globally.
- Run the runner in one-shot mode for verification, inspect collection and
skipped tests, and report the process exit code. Coverage targets follow the
project; they do not replace behavioral assertions.
Choose the right boundary
| Behavior |
Suitable test |
| Form validation, hook results, loading/error UI |
Testing Library with Jest/Vitest |
| Real layout, contrast, scrolling, downloads, native browser behavior |
Browser/component tests in a real browser |
| Login through API/database to another page |
Isolated full-flow browser integration |
DOM accessibility checks catch some issues; JSDOM does not establish visual
contrast, layout correctness or complete accessibility compliance. Use browser
evidence and relevant manual checks for those requirements.
References and prerequisites
Read component test patterns. The skill supplies no
named ECC agents, slash commands or installed packages. Check the project's
dependencies before using Testing Library, MSW or an accessibility checker.
Use skill-router to resolve webapp-testing, react-dev and verification-loop
from the full library when they are not installed locally.
Provenance
Adapted from ECC react-testing.
See MIT notice. Local changes narrow routing, preserve existing
runners, correct accessibility claims and remove ECC-only dependencies.
1---2name: react-testing3description: >-4 Write and repair React component and hook tests with Testing Library and the5 project's Jest or Vitest runner. Use for form behavior, async state, network6 mocks, accessible queries, and deciding between component and browser tests.7license: MIT8metadata:9 source: https://skillmd.com/skills/hongyime/react-testing10 author: Local setup, adapted from ECC11 version: "1.0.0"12 platform: "Codex, Antigravity (agy), OpenCode, Muse on Windows, macOS, Linux (Debian)"13---1415# React Testing1617Assert what users can observe, using the test stack the project already owns.1819## When to use2021Use for component, hook and form behavior tests. Use `react-dev` for implementation22and `webapp-testing` for real layout, navigation and complete browser flows.23Do not switch Jest to Vitest or install a second runner merely to use this guide.2425## Workflow26271. Read the component's contract and repo test configuration. Identify the28 existing runner, DOM environment, setup file, providers and network mocks.29 Preserve project and coursework boundaries. Server components/framework-only30 behavior may require the framework's integration or browser test support.312. Define observable cases: success, loading, empty, error and invalid input32 where they apply. Write an independent expected result. For a bug, confirm33 the regression test fails for the actual behavior before changing code.343. Render through real providers with a fresh cache/store per test. Use35 `getByRole` with an accessible name or `getByLabelText`. Use a test ID only36 when semantic queries cannot express the target.374. Create `userEvent.setup()` per test and await interactions. Use `findBy*`38 for arriving content, `queryBy*` for absence, and `waitFor` for asynchronous39 assertions. Do not put repeated side-effectful clicks inside `waitFor`.405. Mock HTTP at the network boundary with the existing MSW setup when present;41 fail unexpected requests, reset handlers between tests, and test error42 responses. An injected service fake is also valid for a narrow component43 contract; label it honestly as a fake, not a real integration test.446. Assert visible results and relevant external effects. Avoid checking private45 state, render counts or calling internal handlers directly. Restore spies,46 timers and global modifications after each test. Treat act warnings as work47 to investigate, not noise to suppress globally.487. Run the runner in one-shot mode for verification, inspect collection and49 skipped tests, and report the process exit code. Coverage targets follow the50 project; they do not replace behavioral assertions.5152## Choose the right boundary5354| Behavior | Suitable test |55|---|---|56| Form validation, hook results, loading/error UI | Testing Library with Jest/Vitest |57| Real layout, contrast, scrolling, downloads, native browser behavior | Browser/component tests in a real browser |58| Login through API/database to another page | Isolated full-flow browser integration |5960DOM accessibility checks catch some issues; JSDOM does not establish visual61contrast, layout correctness or complete accessibility compliance. Use browser62evidence and relevant manual checks for those requirements.6364## References and prerequisites6566Read [component test patterns](references/patterns.md). The skill supplies no67named ECC agents, slash commands or installed packages. Check the project's68dependencies before using Testing Library, MSW or an accessibility checker.69Use `skill-router` to resolve `webapp-testing`, `react-dev` and `verification-loop`70from the full library when they are not installed locally.7172## Provenance7374Adapted from [ECC react-testing](https://github.com/affaan-m/ECC/blob/e482e579415fde18357cafce70f177ae19fd7f03/skills/react-testing/SKILL.md).75See [MIT notice](LICENSE.txt). Local changes narrow routing, preserve existing76runners, correct accessibility claims and remove ECC-only dependencies.