testing-library
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "testing-library":https://skillpi.com/zh/skills/deepskill-0152adc7f7c97b15fc72a7d57a092a33e735d6f65670956f06291dcc13bbda3a。安装前核对上游来源和版本:https://github.com/TerminalSkills/skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
使用 Testing Library 以用户交互方式测试 UI 组件,基于角色、文本和标签查询而非实现细节;适用于测试 React 组件、以用户为中心的测试、可访问性断言、用户事件模拟、异步测试及相关查询方法。
介绍来源: third_party · TerminalSkills · 1.0.0 参考来源
SkillPi 解读
- 适合谁
- 翻译待完成,请参阅来源文档。
- 软件、账号及付费要求
- 翻译待完成,请参阅来源文档。
- 实测状态
- 未测试
文件浏览
Frontmatter
name: testing-library
description: >-
Test UI components the way users interact with them using Testing Library —
query by role, text, and label instead of implementation details. Use when
someone asks to "test React components", "Testing Library", "user-centric
testing", "test accessibility", "test without implementation details", or
"render and query components in tests". Covers React Testing Library,
queries, user events, async testing, and accessibility assertions.
license: Apache-2.0
compatibility: "React, Vue, Svelte, Angular, Preact. Vitest/Jest."
metadata:
author: terminal-skills
version: "1.0.0"
category: development
tags: ["testing", "react", "components", "accessibility", "testing-library"]Testing Library
Overview
Testing Library tests UI components from the user's perspective — find elements by their accessible role, text content, or label, not by CSS class or test ID. If a user can't find a button, your test shouldn't find it either. This approach catches accessibility issues by default, survives refactors (rename a CSS class and tests still pass), and produces tests that actually prove the UI works.
When to Use
- Testing React/Vue/Svelte components
- Want tests that survive refactoring (no CSS selectors or internal state checks)
- Need to verify accessibility (ARIA roles, labels)
- Testing user interactions (clicks, typing, form submission)
- Integration testing of component behavior (not snapshot testing)
Instructions
Setup
npm install -D @testing-library/react @testing-library/jest-dom @testing-library/user-event
# For Vitest:
npm install -D @testing-library/react vitest happy-dom
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "happy-dom",
setupFiles: ["./tests/setup.ts"],
},
});
// tests/setup.ts — Global test setup
import "@testing-library/jest-dom/vitest";
Query Priority
Testing Library has a priority order for queries — use the highest priority that works:
// Priority 1: Accessible roles (best — tests accessibility too)
screen.getByRole("button", { name: "Submit" });
screen.getByRole("textbox", { name: "Email" });
screen.getByRole("heading", { level: 1 });
// Priority 2: Label text (forms)
screen.getByLabelText("Email address");
// Priority 3: Placeholder text
screen.getByPlaceholderText("Search...");
// Priority 4: Text content
screen.getByText("Welcome back!");
// Priority 5: Display value
screen.getByDisplayValue("user@example.com");
// Priority 6: Alt text (images)
screen.getByAltText("Company logo");
// Last resort: test IDs (avoid if possible)
screen.getByTestId("complex-widget");
Testing Component Behavior
// LoginForm.test.tsx — Test a login form from the user's perspective
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { LoginForm } from "./LoginForm";
describe("LoginForm", () => {
it("submits the form with email and password", async () => {
const onSubmit = vi.fn();
const user = userEvent.setup();
render(<LoginForm onSubmit={onSubmit} />);
// Find elements by their accessible role/label
await user.type(screen.getByLabelText("Email"), "kai@example.com");
await user.type(screen.getByLabelText("Password"), "secret123");
await user.click(screen.getByRole("button", { name: "Sign in" }));
expect(onSubmit).toHaveBeenCalledWith({
email: "kai@example.com",
password: "secret123",
});
});
it("shows validation error for invalid email", async () => {
const user = userEvent.setup();
render(<LoginForm onSubmit={vi.fn()} />);
await user.type(screen.getByLabelText("Email"), "not-an-email");
await user.click(screen.getByRole("button", { name: "Sign in" }));
expect(screen.getByRole("alert")).toHaveTextContent("Invalid email address");
});
it("disables submit while loading", async () => {
render(<LoginForm onSubmit={() => new Promise(() => {})} />);
const user = userEvent.setup();
await user.type(screen.getByLabelText("Email"), "kai@example.com");
await user.type(screen.getByLabelText("Password"), "secret123");
await user.click(screen.getByRole("button", { name: "Sign in" }));
expect(screen.getByRole("button", { name: /signing in/i })).toBeDisabled();
});
});
Async Testing
// UserProfile.test.tsx — Testing async data fetching
import { render, screen, waitFor } from "@testing-library/react";
import { UserProfile } from "./UserProfile";
it("loads and displays user data", async () => {
render(<UserProfile userId="123" />);
// Loading state
expect(screen.getByText("Loading...")).toBeInTheDocument();
// Wait for data to load
await waitFor(() => {
expect(screen.getByRole("heading")).toHaveTextContent("Kai Chen");
});
expect(screen.getByText("kai@example.com")).toBeInTheDocument();
});
it("shows error state on failure", async () => {
// Mock API failure
server.use(http.get("/api/users/123", () => HttpResponse.error()));
render(<UserProfile userId="123" />);
await waitFor(() => {
expect(screen.getByRole("alert")).toHaveTextContent("Failed to load");
});
});
Examples
Example 1: Test a complex form with validation
User prompt: "Write tests for a multi-step registration form with validation."
The agent will write tests that fill each step from the user's perspective, verify validation errors appear on invalid input, and confirm successful submission.
Example 2: Test an accessible data table
User prompt: "Test a sortable data table — verify sorting, filtering, and pagination work."
The agent will query table headers by role, click to sort, verify row order changes, type into filter input, and navigate pages.
Guidelines
- Query by role first —
getByRoletests accessibility for free userEventoverfireEvent— simulates real user behavior (focus, type, blur)userEvent.setup()at test start — creates a user instance for the testwaitForfor async — wait for elements to appear after data fetching- Avoid
getByTestId— if you need it, the component might have accessibility issues screenis global — no need to destructure render resulttoBeInTheDocument()from jest-dom — readable assertions- Don't test implementation — state values, effect triggers, re-render counts
- Test behavior, not structure — "user sees error" not "error div has class active"
当前收录版本
1.0.0
安全与评测信息
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 提供 Testing Library 的完整实践指南,覆盖 React/Vue/Svelte 等主流框架
- 强调用户视角和可访问性测试,具有广泛实用价值
亮点
- 触发词覆盖全面,包括多种常见测试场景表述
- 边界清晰,不易与其他 Skill 混淆
亮点
- 结构完整,包含安装、查询优先级、行为测试、异步测试等关键章节
- 提供端到端示例和最佳实践指南
待提升项
-
description写错位置 原报告严重程度:轻微
顶层 description 字段为空,实际描述内容位于 skill_body 内部的 frontmatter 中
改进建议:建议将 description 内容移至顶层字段,确保触发机制能正确读取
亮点
- 工具来源明确,参数正确,覆盖主要异常场景
- 提供完整的测试设置和断言示例
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
安装或获取外部依赖
使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。
主文档提及 · 第 38, 40 行
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
翻译待完成,请参阅来源文档。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。