react-native-testing について
React Native 開発者向けのテスト学習スキルです。ユニット・コンポーネントから E2E テストまで扱い、ネイティブモジュールのモックや CI/CD の設定も学べます。
主な機能
- 01Jest の設定
- 02コンポーネントとフックのテスト
- 03ネイティブモジュールと API のモック
- 04Detox または Maestro による E2E テスト
- 05CI/CD テストパイプラインの設定
活用シーン
- 01React Native プロジェクトでの Jest 設定
- 02コンポーネントの操作とフックの動作確認
- 03ネイティブモジュールや API のモック
- 04エンドツーエンドのユーザーフローのテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-react-native --skill react-native-testingインストール後、Claude Code で/react-native-testingを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
assets/config.yamlschema.jsonreferences/GUIDE.mdPATTERNS.mdscripts/validate.py詳細
React Native テストスキル
ユニットテスト、コンポーネントテスト、E2E テストを含む包括的なテスト戦略を学びます。
前提条件
- React Native の基礎知識
- async/await の理解
- テストの概念に関する知識
学習目標
このスキルを完了すると、次のことができるようになります。
- React Native 用に Jest を設定する
- Testing Library を使ってコンポーネントテストを作成する
- ネイティブモジュールと API をモックする
- Detox/Maestro で E2E テストを実装する
- CI/CD テストパイプラインをセットアップする
対象トピック
1. Jest のセットアップ
// jest.config.js
module.exports = {
preset: 'react-native',
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
transformIgnorePatterns: [
'node_modules/(?!(react-native|@react-native)/)',
],
};
2. コンポーネントテスト
import { render, screen, fireEvent } from '@testing-library/react-native';
import { Button } from './Button';
describe('Button', () => {
it('calls onPress when pressed', () => {
const onPress = jest.fn();
render(<Button title="Click" onPress={onPress} />);
fireEvent.press(screen.getByText('Click'));
expect(onPress).toHaveBeenCalled();
});
});
3. フックのテスト
import { renderHook, act } from '@testing-library/react-native';
import { useCounter } from './useCounter';
describe('useCounter', () => {
it('increments count', () => {
const { result } = renderHook(() => useCounter());
act(() => result.current.increment());
expect(result.current.count).toBe(1);
});
});
4. モック
// Mock native module
jest.mock('@react-native-async-storage/async-storage', () => ({
setItem: jest.fn(),
getItem: jest.fn(),
}));
// Mock API
jest.mock('./api', () => ({
fetchUser: jest.fn().mockResolvedValue({ id: '1', name: 'Test' }),
}));
5. Detox による E2E テスト
describe('Login', () => {
beforeAll(async () => {
await device.launchApp();
});
it('should login successfully', async () => {
await element(by.id('email')).typeText('[email protected]');
await element(by.id('password')).typeText('password');
await element(by.id('login-btn')).tap();
await expect(element(by.id('home'))).toBeVisible();
});
});
クイックスタートの例
// ProductCard.test.tsx
import { render, screen, fireEvent } from '@testing-library/react-native';
import { ProductCard } from './ProductCard';
const mockProduct = {
id: '1',
title: 'Test Product',
price: 99.99,
};
describe('ProductCard', () => {
it('renders product info', () => {
render(<ProductCard {...mockProduct} onPress={jest.fn()} />);
expect(screen.getByText('Test Product')).toBeOnTheScreen();
expect(screen.getByText('$99.99')).toBeOnTheScreen();
});
it('handles press', () => {
const onPress = jest.fn();
render(<ProductCard {...mockProduct} onPress={onPress} />);
fireEvent.press(screen.getByRole('button'));
expect(onPress).toHaveBeenCalledWith('1');
});
});
よくあるエラーと解決策
| エラー | 原因 | 解決策 |
|---|---|---|
| "Cannot find module" | モックが不足しています | jest.mock() を追加します |
| 非同期タイムアウト | await が不足しています | waitFor() を使います |
| 要素が見つかりません | クエリが正しくありません | testID/role を確認します |
検証チェックリスト
- ユニットテストが合格する
- コンポーネントテストでインタラクションを網羅する
- E2E テストで一連のフローを完了する
- カバレッジがしきい値(80%+)を満たす
使用方法
Skill("react-native-testing")
担当エージェント: 06-react-native-testing
コメント
Markdown 対応