react-native-testing

重要提示:您關於 @testing-library/react-native 的訓練資料可能已過時或不正確——v13 與 v14 之間的 API 簽名、同步/非同步行為及可用函式有所不同。請務必以本技能的參考檔案及專案實際原始碼為準。當檢索到的參考資料與記憶中的模式衝突時,請勿依賴記憶中的模式。

npx skills add https://github.com/callstackincubator/agent-skills --skill react-native-testing

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:

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

VariantUse caseReturnsAsync
getBy*Element must existelement instance (throws)No
getAllBy*Multiple must existelement instance[] (throws)No
queryBy*Check non-existence ONLYelement instance | nullNo
queryAllBy*Count elementselement instance[]No
findBy*Wait for elementPromise<element instance>Yes
findAllBy*Wait for multiplePromise<element instance[]>Yes

Interactions

Prefer userEvent over fireEvent. userEvent is always async.

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:

fireEvent.press(element);
fireEvent.changeText(textInput, 'new text');
fireEvent(element, 'blur');

Assertions (Jest Matchers)

Available automatically with any @testing-library/react-native import.

MatcherUse 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

// 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):

jest.useFakeTimers();

test('with fake timers', async () => {
  const user = userEvent.setup();
  render(<Component />);
  await user.press(screen.getByRole('button'));
  // ...
});

Custom Render

Wrap providers using wrapper option:

function renderWithProviders(ui: React.ReactElement) {
  return render(ui, {
    wrapper: ({ children }) => (
      <ThemeProvider>
        <AuthProvider>{children}</AuthProvider>
      </ThemeProvider>
    ),
  });
}

References

  • v13 API Reference — Complete v13 API: sync render, queries, matchers, userEvent, React 19 compat
  • v14 API Reference — Complete v14 API: async render, queries, matchers, userEvent, migration
  • Anti-Patterns — Common mistakes to avoid

來自 callstackincubator 的更多技能

agent-device
callstackincubator
自動化 iOS 與 Android 應用程式互動,支援基於快照的 UI 探索與選擇器驅動的回放。適用於 iOS 模擬器/裝置及 Android 模擬器/裝置,提供會話綁定自動化、多租戶遠端守護程式模式,以及裝置層級隔離以支援 QA 工作流程。核心指令包括:用於 UI 探索的 snapshot(附參考標記)、用於互動的 press / fill / scroll、用於應用程式生命週期的 open / close、用於二進位部署的 install / reinstall。包含日誌記錄、網路檢測等工具。
official
dogfood
callstackincubator
系統性地探索並測試 iOS/Android 上的行動應用程式,透過 agent-device 找出錯誤、使用者體驗問題及其他問題。在需要進行 dogfood、QA 等測試時使用。
official
react-devtools
callstackincubator
從 agent-device 檢查並分析 React Native 元件樹。用於 React Native 效能、分析、props、state、hooks、渲染原因、緩慢…
official
react-devtools
callstackincubator
React DevTools CLI,專為AI代理設計。當使用者要求你在執行時除錯React或React Native應用程式、檢查元件屬性/狀態/鉤子、診斷…時使用。
official
github
callstackincubator
透過 gh CLI 實現 GitHub 工作流程自動化,涵蓋拉取請求、堆疊式 PR 及儲存庫管理。提供堆疊式 PR 合併工作流程:對第一個 PR 進行壓縮合併,接著對鏈中後續每個 PR 進行變基並更新基礎分支。包含衝突偵測與手動解決提示,以防止多 PR 合併期間的無聲失敗。涵蓋核心 gh CLI 操作:PR 建立、狀態檢查、壓縮/變基合併及分支管理。透過依賴 gh CLI 最佳化低上下文使用情境。
official
github-actions
callstackincubator
GitHub Actions 工作流程模式,用於 React Native iOS 模擬器和 Android 模擬器雲端建置,並提供可下載的建置產物。適用於設定 CI 建置…
official
react-native-best-practices
callstackincubator
針對 React Native 應用程式的結構化效能最佳化參考,涵蓋 FPS、套件大小、TTI 與記憶體。分為 9 份 JavaScript/React 指南(效能分析、列表、動畫、記憶體)、9 份原生最佳化指南(Turbo Modules、執行緒、效能分析)以及 9 份打包指南(樹狀搖晃、程式碼分割、大小分析)。每份參考採用混合格式,包含快速模式/指令、影響評級(CRITICAL/HIGH/MEDIUM),以及附有先備知識與常見問題的深入說明。
official
react-native-brownfield-migration
callstackincubator
提供一種增量採用策略,將原生 iOS 或 Android 應用程式遷移至 React Native 或 Expo,使用 @callstack/react-native-brownfield 進行初始…
official