React component testing with React Testing Library, Vitest/Jest, MSW for network mocking, accessibility assertions with axe, and the decision boundary betwee…
Pins this version in aipm.package.json and aipm-lock.json.
aipm add @affaan-m/react-testing@1.0.0
Without project tracking
Writes the skill files only. Add --no-init to skip aipm.package.json and the lockfile.
aipm add @affaan-m/react-testing@1.0.0 --no-init
How to use
Install the skill above, then run this in your AI tool:
/react-testing <your description>
About
Comprehensive React testing patterns for behavior-focused component tests, custom hook tests, accessibility assertions, and network-level mocking.
Source
What gets added to your project
Preview every file this package writes on install before you run the command above.
Summary
React component testing with React Testing Library, Vitest/Jest, MSW for network mocking, accessibility assertions with axe, and the decision boundary between component tests and Playwright/Cypress end-to-end runs. Use when writing or fi...
Skill instructions
React Testing
Comprehensive React testing patterns for behavior-focused component tests, custom hook tests, accessibility assertions, and network-level mocking.
When to Activate
Writing tests for React components, custom hooks, or pages
Adding test coverage to legacy untested components
Migrating from Enzyme or class-component-era patterns to React Testing Library
Setting up Vitest or Jest for a new React project
Mocking HTTP requests in tests
Asserting accessibility violations
Deciding which tests belong in RTL vs Playwright Component Testing vs full E2E
Core Principle
Test what the user sees and does, not implementation details.
A test should:
Render the component with the same providers it has in production
Interact with it via accessible queries (role, label) and userEvent
Assert visible output and observable side effects (callback fired, request sent)
A test should NOT:
Inspect component state, props passed to children, or which hooks were called
Mock React itself or framework hooks
Assert on the number of renders or DOM structure beyond what affects users
Library Choice
Runner
When
Note
Vitest
Vite, Remix, modern setups
Faster, native ESM, Jest-compatible API
Jest
Next.js, CRA, established repos
Default for many React projects
Playwright Component Testing
Real browser engine needed
Use when JSDOM lacks the required feature
Cypress Component Testing
Real browser, Cypress already in use
Alternative to Playwright CT
Pick one. Do not run RTL + Vitest AND Playwright CT in the same repo unless you have a clear lane separation.
Query Priority
React Testing Library exposes queries in three tiers — use top-down:
Accessible to everyone: getByRole, getByLabelText, getByPlaceholderText, getByText, getByDisplayValue
Semantic: getByAltText, getByTitle
Test IDs (escape hatch): getByTestId
tsx
// Best
screen.getByRole("button", { name: /save/i });
// OK for inputs
screen.getByLabelText("Email");
// Last resort
screen.getByTestId("save-btn");
Variants:
getBy* — throws if no match
queryBy* — returns null (use for "assert absence")
findBy* — async, returns a Promise (use for elements that appear after async work)
Call userEvent.setup() once per test, reuse the returned user
userEvent simulates a real browser sequence; fireEvent dispatches a single synthetic event — prefer userEvent
Async Patterns
tsx
// Element that appears after async workexpect(await screen.findByText("Loaded")).toBeInTheDocument();
// Side effect assertionawaitwaitFor(() =>expect(saveSpy).toHaveBeenCalled());
// Element that should disappearawaitwaitForElementToBeRemoved(() => screen.queryByText("Loading"));
Never setTimeout + assertion — flaky. Use the matchers above.
Network Mocking with MSW
Mock Service Worker mocks at the network layer. The component, hooks, and fetch library all behave exactly as in production.
container.querySelector("...") — bypasses accessibility queries, lets tests pass when real users would fail
Asserting on number of renders — implementation detail
jest.mock("react", ...) — never mock React. Refactor the component instead
Mocking child components by default — tests the integration, not isolation. Mock only when the child has heavy side effects
Ignoring act() warnings — they signal real bugs (state update after unmount, missing async wrapping)
Sharing mutable state across tests — flakes when test order changes
Tests that pass with it.skip() removed — your test does not actually assert what you think
TDD Workflow
code
RED -> Write failing test for the next requirement
GREEN -> Write minimal component code to pass
REFACTOR -> Improve the component, tests stay green
REPEAT -> Next requirement
For new components:
Define the component's prop type and signature
Write the first test for the simplest case
Verify it fails for the right reason
Implement just enough to pass
Add the next test case
Refactor when the third similar test reveals a pattern
Test Commands
bash
# Vitest
vitest # watch
vitest run # one-shot
vitest run --coverage # with coverage
vitest run path/to/file.test.tsx # single file# Jest
jest --watch
jest --coverage
jest path/to/file.test.tsx
# CI mode
CI=true vitest run --coverage
Add this badge to a GitHub README so readers can open the package page and install the same version.
[](https://www.aipm-registry.com/skills/affaan-m/react-testing/1.0.0?utm_source=github&utm_medium=readme&utm_campaign=package_badge)
This imported publisher has not claimed its AIPM account. A connected account confirms account control, but it does not guarantee that a package is safe.
AI assistant context
What this skill is for
@affaan-m/react-testing@1.0.0 is an AIPM skill package for All tools. Use it when a project needs the reusable AI behavior described as: React component testing with React Testing Library, Vitest/Jest, MSW for network mocking, accessibility assertions with axe, and the decision boundary between component tests and Playwright/Cypress end-to-end runs. Use when writing or fi...
Preview a UI component mockup in a local browser canvas where the human directly edits it (move, resize, edit text, delete, restyle), switches mobile/desktop/custom viewports, tests it, chats with you, and clicks Save and Finish. You the...
Run aipm add @affaan-m/react-testing@1.0.0 in a project that has AIPM initialized.
Supported tools
@affaan-m/react-testing@1.0.0 supports All tools.
Before installing
AIPM skills can add files to your project. Review the package name, target, description, license, and publisher before installing. Use a clean branch when trying a new skill.