testing-library

Testing Library user-centric testing. Use for React testing.

7 stars

Best use case

testing-library is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Testing Library user-centric testing. Use for React testing.

Teams using testing-library should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/testing-library/SKILL.md --create-dirs "https://raw.githubusercontent.com/G1Joshi/Agent-Skills/main/skills/testing/testing-library/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/testing-library/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How testing-library Compares

Feature / Agenttesting-libraryStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Testing Library user-centric testing. Use for React testing.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Testing Library (React/DOM)

The guiding principle: **"The more your tests resemble the way your software is used, the more confidence they can give you."**
It encourages testing _behavior_ (what the user sees) rather than implementation details (state, props).

## When to Use

- **React Components**: The industry standard (`@testing-library/react`).
- **Accessibility Testing**: It inherently encourages accessible code (`getByRole`).

## Quick Start

```javascript
import { render, screen, fireEvent } from "@testing-library/react";
import App from "./App";

test("renders learn react link", () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
});

test("button click", () => {
  render(<Button />);
  const btn = screen.getByRole("button", { name: /submit/i });
  fireEvent.click(btn);
  expect(btn).toBeDisabled();
});
```

## Core Concepts

### Queries (Priority)

1. `getByRole`: Best. Tests accessibility structure (button, heading).
2. `getByLabelText`: Good for forms.
3. `getByText`: Good for verifying content.
4. `getByTestId`: **Last Resort**. Only use if nothing else targets the element stable.

### User Event

`fireEvent` is low level. Use `@testing-library/user-event` to simulate real interactions (typing, hovering).

```javascript
import userEvent from "@testing-library/user-event";
await userEvent.click(screen.getByRole("button"));
```

## Best Practices (2025)

**Do**:

- **Use `screen`**: Always import `screen` for global queries.
- **Use `await findBy...`**: For async elements (fetching data). `getBy` fails immediately if not found.
- **Test User Flows**: Don't test valid state; test "User clicks button -> Text appears".

**Don't**:

- **Don't use `container.querySelector`**: Defeats the purpose.
- **Don't test implementation**: Don't check the component's state or class methods directly.

## References

- [Testing Library Docs](https://testing-library.com/)