component-testing
БесплатноБез исполняемых скриптовНе проверенUse when writing or changing any React component - React Testing Library tests are behavior-focused (byRole first), use user-event, assert on accessible…
от makifbaysal★ 123 звезды на GitHubОб этом скилле
Component Testing (React Testing Library)
Overview
A component test that queries by data-testid or asserts on internal state proves the implementation didn't change, not that the component works for a user. React Testing Library tests must exercise the component the way a user (and a screen reader) does: find things by role/label/text, interact with them, and assert on what actually rendered.
Core principle: Test behavior, not implementation. If a refactor that keeps the same user-facing behavior breaks the test, the test is coupled to the wrong thing.
Rules
- Query priority:
byRolefirst. PrefergetByRole(with{ name: ... }) overbyLabelText/byText, and both overbyTestId.byTestIdis a last resort for a node with no accessible role or text — reach for it only when nothing else identifies the element, and treat its presence as a smell worth a follow-up (usually a missing label/role, per accessibility-basics). user-eventoverfireEvent.fireEvent.clickfires one DOM event;userEvent.clicksimulates the real sequence of events a browser produces (focus, pointer, keyboard) and catches bugsfireEventcannot (e.g. a control that only works because it happens to be focused). Alwaysawait userEvent.setup()/await user.click(...).- Assert on accessible, user-visible output. Check rendered text, roles,
aria-*state (aria-expanded,aria-invalid,aria-disabled), and focus — not component internals, prop values, or CSS class names. - One behavior per test. A test named for the behavior it proves (
"shows a validation error when the field is left empty"), not"renders correctly". Multiple assertions are fine as long as they all check the same behavior; a second, unrelated behavior gets its own test. - No implementation-detail mocking. Mock the network/API boundary (MSW or the API client module), never a child component or a hook's internals just to make the test pass — that tests the mock, not the component.
- Wire it into
npm test. New component test files must run under the project's existingnpm testscript (Vitest/Jest) with no extra flags — a suite that only runs when invoked by hand is not part of the pipeline (see ci-cd-pipeline-authoring). - TDD still applies (see tdd-workflow): write the test against the intended behavior first, watch it fail, then implement.
Worked Example
// ❌ implementation-coupled: breaks on any internal refactor, proves nothing
// about what the user sees
test("renders correctly", () => {
render(<LoginForm />);
expect(screen.getByTestId("submit-btn")).toBeInTheDocument();
});
// ✅ behavior-focused: fails only when the real user-facing behavior breaks
test("shows a validation error when submitting with an empty email", async () => {
const user = userEvent.setup();
render(<LoginForm />);
await user.click(screen.getByRole("button", { name: /sign in/i }));
expect(screen.getByRole("alert")).toHaveTextContent(/email is required/i);
expect(screen.getByRole("textbox", { name: /email/i })).toHaveAttribute("aria-invalid", "true");
});
The second test finds the button and field the way a user would (by their accessible name), drives them with user-event, and asserts on what actually appears — it stays green through any internal rewrite that keeps this behavior.
Common Mistakes
- Querying by
data-testidwhen a role/label query would work — usually a sign the component is missing an accessible name in the first place. - Using
fireEventfor anything a real user would do with a mouse or keyboard. - One giant test asserting the whole component "renders correctly" instead of named tests per behavior.
- Snapshot tests as the only coverage — they fail on any markup change, meaningful or not, and don't document behavior.
- Mocking a child component to isolate the parent, hiding real integration bugs.
- A new
*.test.tsxfile thatnpm testnever picks up (wrong glob, wrong location) — decoration, not a gate.
Red Flags
screen.getByTestId(...)with no comment on why no role/label query worked.fireEventanywhere auser-eventequivalent exists.- A test file with only snapshot assertions.
- Assertions on a component's props/state instead of its rendered output.
- Test suite green locally but
npm testin CI never runs the new file.
Установить component-testing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл component-testing?
Use when writing or changing any React component - React Testing Library tests are behavior-focused (byRole first), use user-event, assert on accessible output, one behavior per test, and run in npm test
Как установить скилл component-testing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл component-testing запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic