--- name: device-testing description: Expert in React Native testing strategies including unit tests with Jest, integration tests, E2E tests with Detox, component testing with React Native Testing Library, snapshot testing, mocking native modules, testing on simulators and real devices. Activates for testing, jest, detox, e2e, unit test, integration test, component test, test runner, mock, snapshot test, testing library, react native testing library, test automation. --- # Device Testing Expert Comprehensive expertise in React Native testing strategies, from unit tests to end-to-end testing on real devices and simulators. Specializes in Jest, Detox, React Native Testing Library, and mobile testing best practices. ## What I Know ### Testing Pyramid for Mobile **Three Layers** 1. **Unit Tests** (70%): Fast, isolated, test logic 2. **Integration Tests** (20%): Test component integration 3. **E2E Tests** (10%): Test full user flows on devices **Tools** - **Jest**: Unit and integration testing - **React Native Testing Library**: Component testing - **Detox**: E2E testing on simulators/emulators - **Maestro**: Alternative E2E testing (newer) ### Unit Testing with Jest **Basic Component Test** ```javascript // UserProfile.test.js import React from 'react'; import { render, fireEvent } from '@testing-library/react-native'; import UserProfile from './UserProfile'; describe('UserProfile', () => { it('renders user name correctly', () => { const user = { name: 'John Doe', email: 'john@example.com' }; const { getByText } = render(); expect(getByText('John Doe')).toBeTruthy(); expect(getByText('john@example.com')).toBeTruthy(); }); it('calls onPress when button is pressed', () => { const onPress = jest.fn(); const { getByText } = render( ); fireEvent.press(getByText('Edit Profile')); expect(onPress).toHaveBeenCalledTimes(1); }); }); ``` **Testing Hooks** ```javascript // useCounter.test.js import { renderHook, act } from '@testing-library/react-hooks'; import useCounter from './useCounter'; describe('useCounter', () => { it('increments counter', () => { const { result } = renderHook(() => useCounter()); act(() => { result.current.increment(); }); expect(result.current.count).toBe(1); }); it('decrements counter', () => { const { result } = renderHook(() => useCounter(5)); act(() => { result.current.decrement(); }); expect(result.current.count).toBe(4); }); }); ``` **Async Testing** ```javascript // api.test.js import { fetchUser } from './api'; describe('fetchUser', () => { it('fetches user data successfully', async () => { const user = await fetchUser('123'); expect(user).toEqual({ id: '123', name: 'John Doe', email: 'john@example.com' }); }); it('handles errors gracefully', async () => { await expect(fetchUser('invalid')).rejects.toThrow('User not found'); }); }); ``` **Snapshot Testing** ```javascript // Button.test.js import React from 'react'; import { render } from '@testing-library/react-native'; import Button from './Button'; describe('Button', () => { it('renders correctly', () => { const { toJSON } = render(