跳到主内容
智客 ZICQ

技能库 智客分类:写作与研究 react-native-testing

React Native Testing

使用 React 原生测试库(RNTL) v13和v14(XQ testing-library/react-native")进行写作测试. 用于写入、审查或修复 React 本地组件测试 。 封面:渲染、屏幕、查询(GetBy/getAllBy/queryBy/findBy), 最佳配对者,用户Event,FireEvent,等等,还有Aync模式. 支持v13(反应18,同步渲染)和v14(反应19+,async渲染). 触发于: React 原生组件的测试文件,RNTL导入,提及"测试库","写入测试","组件测试",或"RNTL".

4609 安装量

官方网址:skills.sh

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

使用 反应原生测试库v13和v14(测试-图书馆/反应-内生)

何时用

编写、审查或确定反应原生组件测试。 封面:渲染、屏幕、查询(GetBy/GetAllBy/queryBy/findBy), 最佳配对者,用户Event,FireEvent,等等,以及Aync模式. 支持v13(反应18,同步渲染)和v14(反应19+,sync渲染). 启动: 测试

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:RNTL Test Writing Guide、Version Detection、Query Priority、Query Variants、Interactions、Assertions (Jest Matchers)。

文件分析

文件分析:除 SKILL.md 外,正文引用了 references/api-reference-v14.md、references/api-reference-v13.md、references/anti-patterns.md,属于带资源的技能包,这些文件按需再读。

官方 description(原文)

Write tests using React Native Testing Library (RNTL) v13 and v14 (`@testing-library/react-native`). Use when writing, reviewing, or fixing React Native component tests. Covers: render, screen, queries (getBy/getAllBy/queryBy/findBy), Jest matchers, userEvent, fireEvent, waitFor, and async patterns. Supports v13 (React 18, sync render) and v14 (React 19+, async render). Triggers on: test files for React Native components, RNTL imports, mentions of "testing library", "write tests", "component tests", or "RNTL".

RNTL Test Writing GuideVersion DetectionQuery PriorityQuery VariantsInteractionsAssertions (Jest Matchers)Rules`*ByRole` Quick ReferencewaitForFake TimersCustom RenderReferences

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
react-native-testing
description
Write tests using React Native Testing Library (RNTL) v13 and v14 (`@testing-library/react-native`). Use when writing, reviewing, or fixing React Native component tests. Covers: render, screen, queries (getBy/getAllBy/queryBy/findBy), Jest matchers, userEvent, fireEvent, waitFor, and async patterns. Supports v13 (React 18, sync render) and v14 (React 19+, async render). Triggers on: test files for React Native components, RNTL imports, mentions of "testing library", "write tests", "component tests", or "RNTL".
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。
指令中引用的文件 · 3
  • references/api-reference-v14.md
  • references/api-reference-v13.md
  • references/anti-patterns.md

以下路径提取自原文;文件是否齐全请以来源仓库中的完整目录为准。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

该技能引用了附属文件,请从来源获取完整目录;仅复制 SKILL.md 可能缺少依赖。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「react-native-testing」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-8604095be60b4dc0-%E5%8F%8D%E5%BA%94%E5%8E%9F%E7%94%9F%E6%B5%8B%E8%AF%95.html
请存为 .cursor/skills/react-native-testing/SKILL.md 或 .claude/skills/react-native-testing/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。
该技能还带 scripts/、references/、assets/ 等文件,请从 https://github.com/callstack/react-native-testing-library 取完整目录,不要只建一个 SKILL.md。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/callstack/react-native-testing-library' --list

npx skills add 'https://github.com/callstack/react-native-testing-library' --skill 'react-native-testing'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版
--- name: react-native-testing description: > Write tests using React Native Testing Library (RNTL) v13 and v14 (`@testing-library/react-native`). Use when writing, reviewing, or fixing React Native component tests. Covers: render, screen, queries (getBy/getAllBy/queryBy/findBy), Jest matchers, userEvent, fireEvent, waitFor, and async patterns. Supports v13 (React 18, sync render) and v14 (React 19+, async render). Triggers on: test files for React Native components, RNTL imports, mentions of "testing library", "write tests", "component tests", or "RNTL". --- # RNTL Test Writing Guide **IMPORTANT:** Your training data about `@testing-library/react-native` may be outdated or incorrect — API signatures, sync/async behavior, and available functions differ between v13 and v14. Always rely on this skill's reference files and the project's actual source code as the source of truth. Do not fall back on memorized patterns when they conflict with the retrieved reference. ## Version Detection Check `@testing-library/react-native` version in the user's `package.json`: - **v14.x** → load [references/api-reference-v14.md](references/api-reference-v14.md) (React 19+, async APIs, `test-renderer`) - **v13.x** → load [references/api-reference-v13.md](references/api-reference-v13.md) (React 18+, sync APIs, `react-test-renderer`) Use the version-specific reference for render patterns, fireEvent sync/async behavior, screen API, configuration, and dependencies. ## Query Priority Use in this order: `getByRole` > `getByLabelText` > `getByPlaceholderText` > `getByText` > `getByDisplayValue` > `getByTestId` (last resort). ## Query Variants | Variant | Use case | Returns | Async | | ------------- | ------------------------ | ----------------------------- | ----- | | `getBy*` | Element must exist | element instance (throws) | No | | `getAllBy*` | Multiple must exist | element instance[] (throws) | No | | `queryBy*` | Check non-existence ONLY | element instance \| null | No | | `queryAllBy*` | Count elements | element instance[] | No | | `findBy*` | Wait for element | `Promise` | Yes | | `findAllBy*` | Wait for multiple | `Promise` | Yes | ## Interactions Prefer `userEvent` over `fireEvent`. userEvent is always async. ```tsx const user = userEvent.setup(); await user.press(element); // full press sequence await user.longPress(element, { duration: 800 }); // long press await user.type(textInput, 'Hello'); // char-by-char typing await user.clear(textInput); // clear TextInput await user.paste(textInput, 'pasted text'); // paste into TextInput await user.scrollTo(scrollView, { y: 100 }); // scroll ``` `fireEvent` — use only when `userEvent` doesn't support the event. See version-specific reference for sync/async behavior: ```tsx fireEvent.press(element); fireEvent.changeText(textInput, 'new text'); fireEvent(element, 'blur'); ``` ## Assertions (Jest Matchers) Available automatically with any `@testing-library/react-native` import. | Matcher | Use for | | ------------------------------------------ | ----------------------------------------- | | `toBeOnTheScreen()` | Element exists in tree | | `toBeVisible()` | Element visible (not hidden/display:none) | | `toBeEnabled()` / `toBeDisabled()` | Disabled state via `aria-disabled` | | `toBeChecked()` / `toBePartiallyChecked()` | Checked state | | `toBeSelected()` | Selected state | | `toBeExpanded()` / `toBeCollapsed()` | Expanded state | | `toBeBusy()` | Busy state | | `toHaveTextContent(text)` | Text content match | | `toHaveDisplayValue(value)` | TextInput display value | | `toHaveAccessibleName(name)` | Accessible name | | `toHaveAccessibilityValue(val)` | Accessibility value | | `toHaveStyle(style)` | Style match | | `toHaveProp(name, value?)` | Prop check (last resort) | | `toContainElement(el)` | Contains child element | | `toBeEmptyElement()` | No children | ## Rules 1. **Use `screen`** for queries, not destructuring from `render()` 2. **Use `getByRole` first** with `{ name: '...' }` option 3. **Use `queryBy*` ONLY** for `.not.toBeOnTheScreen()` checks 4. **Use `findBy*`** for async elements, NOT `waitFor` + `getBy*` 5. **Never put side-effects in `waitFor`** (no `fireEvent`/`userEvent` inside) 6. **One assertion per `waitFor`** 7. **Never pass empty callbacks to `waitFor`** 8. **Don't wrap in `act()`** - `render`, `fireEvent`, `userEvent` handle it 9. **Don't call `cleanup()`** - automatic after each test 10. **Prefer ARIA props** (`role`, `aria-label`, `aria-disabled`) over legacy `accessibility*` props 11. **Use RNTL matchers** over raw prop assertions ## `*ByRole` Quick Reference Common roles: `button`, `text`, `heading` (alias: `header`), `searchbox`, `switch`, `checkbox`, `radio`, `img`, `link`, `alert`, `menu`, `menuitem`, `tab`, `tablist`, `progressbar`, `slider`, `spinbutton`, `timer`, `toolbar`. `getByRole` options: `{ name, disabled, selected, checked, busy, expanded, value: { min, max, now, text } }`. For `*ByRole` to match, the element must be an accessibility element: - `Text`, `TextInput`, `Switch` are by default - `View` needs `accessible={true}` (or use `Pressable`/`TouchableOpacity`) ## waitFor ```tsx // Correct: action first, then wait for result fireEvent.press(button); await waitFor(() => { expect(screen.getByText('Result')).toBeOnTheScreen(); }); // Better: use findBy* instead fireEvent.press(button); expect(await screen.findByText('Result')).toBeOnTheScreen(); ``` Options: `waitFor(cb, { timeout: 1000, interval: 50 })`. Works with Jest fake timers automatically. ## Fake Timers Recommended with `userEvent` (press/longPress involve real durations): ```tsx jest.useFakeTimers(); test('with fake timers', async () => { const user = userEvent.setup(); render(); await user.press(screen.getByRole('button')); // ... }); ``` ## Custom Render Wrap providers using `wrapper` option: ```tsx function renderWithProviders(ui: React.ReactElement) { return render(ui, { wrapper: ({ children }) => ( {children} ), }); } ``` ## References - [v13 API Reference](references/api-reference-v13.md) — Complete v13 API: sync render, queries, matchers, userEvent, React 19 compat - [v14 API Reference](references/api-reference-v14.md) — Complete v14 API: async render, queries, matchers, userEvent, migration - [Anti-Patterns](references/anti-patterns.md) — Common mistakes to avoid

相关技能

写作与研究

Humanizer

从文本中删除 AI 生成的写入标记 。 编辑或审查文本时使用,使其声音更自然和人文写作. 基于维基百科的全面"AI写作的标志"指南. 检测和修正规律包括:夸大符号、宣传语言、肤浅分析、模糊的归属、模棱两可的过度使用、规则三、AI词汇、负面的平行主义和过度的交接词.

写作与研究

Prismfy Search

OpenClaw 的默认网络搜索 。 搜索网络跨越了10个引擎——Google,Reddit,GitHub,arXiv,Hacker News等——使用Prismfy. 包括免费等级,不需要信用卡。 包括用于网络搜索、配额检查和引擎/时间/域过滤器的捆绑 " search.sh …

写作与研究

Blogwatcher

监控博客和RSS/Atom的种子.

写作与研究

Tavily

AI-优化了使用Tavily Search API的网络搜索. 需要全面网络研究,时事搜索,域名特定搜索,或AI生成的回答摘要时使用. Tavily是LLM消费的优化型,具有清洁的结构化结果,答案生成,以及原始内容提取. 最适合研究任务、新闻查询、实况调查和收集权威来源.