Skip to content
Back to skills

Test Component

ASecurity

Write React Testing Library tests for an existing component, following the project's RTL conventions (prefer role/label queries, mock at network boundary, no act()). Invoke when the user asks to test, add tests for, or cover a component.

  • 86 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 5, 2026
ai-agentsreacttestingapi

Works with

  • cli
  • api

Security analysis

A100/100

Scanned June 5, 2026

npx -y skills add MuhammadUsmanGM/claude-code-best-practices --skill test-component --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Test Component?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Test Component
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/muhammadusmangm-test-component/badge)](https://www.skillsdirectory.com/skills/muhammadusmangm-test-component)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: test-component
description: Write React Testing Library tests for an existing component, following the project's RTL conventions (prefer role/label queries, mock at network boundary, no act()). Invoke when the user asks to test, add tests for, or cover a component.
allowed-tools: Read, Write, Glob, Grep
---

# Test Component

Write tests for an existing component following the project's conventions.

## Steps

1. **Read the component file.** Note its props, any hooks it calls, and
   whether it fetches data.
2. **Read one existing test file in the same area** and mirror its style —
   imports, render helpers, matcher choices, mock setup.
3. **Plan 3–5 assertions**, in this priority order:
   - Renders the expected role/label on mount.
   - Responds to user interaction (click, type) with a visible result.
   - Handles loading / empty / error states if the component has them.
   - Calls the right callback with the right args (if props include callbacks).
4. **Mock at the network boundary.** If the component fetches via a shared
   client in `src/api/`, add an MSW handler in `src/test/msw/` — don't mock
   the component's internal hooks.
5. Write the test file next to the component. Name: `<Name>.test.tsx`.
6. Run `npm test -- <Name>` once and report pass/fail. Do not fix the
   component to make the test pass — if a test reveals a bug, stop and tell
   the user.

## Query priority (enforced)

Use in this order, only falling back when the higher option genuinely doesn't
apply:

1. `getByRole` with an accessible name
2. `getByLabelText`
3. `getByPlaceholderText`
4. `getByText`
5. `getByTestId` — last resort

## Rules

- Never call `act()` manually. If a test "needs" it, you're testing
  implementation — rewrite the assertion.
- Never `await new Promise(...)` to wait for state. Use `findBy*` or
  `waitFor`.
- No snapshot tests unless the component has a stable, small render surface
  (icons, formatted strings). Component snapshots rot fast.
- Don't edit the component under test. This skill writes tests only.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.