testing-library

Testing Library (React/DOM)

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "testing-library" with this command: npx skills add g1joshi/agent-skills/g1joshi-agent-skills-testing-library

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

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)

  • getByRole : Best. Tests accessibility structure (button, heading).

  • getByLabelText : Good for forms.

  • getByText : Good for verifying content.

  • 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).

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

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

template

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

mariadb

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

claude

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

javascript

No summary provided by upstream source.

Repository SourceNeeds Review