|
| 1 | +# RNTL v14: Make render(), act(), renderHook(), and fireEvent() calls async |
| 2 | + |
| 3 | +This codemod migrates your test files from React Native Testing Library v13 to v14 by automatically transforming synchronous function calls to async versions and making test functions async when needed. |
| 4 | + |
| 5 | +## What it does |
| 6 | + |
| 7 | +- Transforms `render()`, `act()`, `renderHook()`, and `fireEvent()` calls to `await render()`, `await act()`, etc. |
| 8 | +- Makes test functions (`test()`, `it()`, hooks) async when needed |
| 9 | +- Handles `fireEvent.press()`, `fireEvent.changeText()`, `fireEvent.scroll()`, `screen.rerender()`, `screen.unmount()`, and renderer methods |
| 10 | +- Only transforms calls imported from `@testing-library/react-native` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```bash |
| 15 | +# Run the codemod |
| 16 | +npx codemod@latest run rntl-v14-async-functions --target ./path/to/your/tests |
| 17 | +``` |
| 18 | + |
| 19 | +### Custom render functions |
| 20 | + |
| 21 | +If you have custom render helper functions (like `renderWithProviders`, `renderWithTheme`), specify them so they get transformed too: |
| 22 | + |
| 23 | +```bash |
| 24 | +npx codemod@latest run rntl-v14-async-functions --target ./path/to/your/tests --param customRenderFunctions="renderWithProviders,renderWithTheme" |
| 25 | +``` |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +**Before:** |
| 30 | + |
| 31 | +```typescript |
| 32 | +test('renders component', () => { |
| 33 | + render(<MyComponent />); |
| 34 | + expect(screen.getByText('Hello')).toBeOnTheScreen(); |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +**After:** |
| 39 | + |
| 40 | +```typescript |
| 41 | +test('renders component', async () => { |
| 42 | + await render(<MyComponent />); |
| 43 | + expect(screen.getByText('Hello')).toBeOnTheScreen(); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Limitations |
| 48 | + |
| 49 | +- Helper functions are not transformed by default (use `customRenderFunctions` param if needed) |
| 50 | +- Namespace imports (`import * as RNTL`) are not handled |
| 51 | + |
| 52 | +## Next steps |
| 53 | + |
| 54 | +1. Run the codemod on your test files |
| 55 | +2. Review the changes |
| 56 | +3. Manually update any remaining helper functions if needed |
| 57 | +4. Update your RNTL version to v14 (`rntl-v14-update-deps` codemod) |
| 58 | +5. Run your tests to verify everything works |
0 commit comments