Skip to content

Commit 367e315

Browse files
chore: v14 codemods (#1850)
1 parent 314e9ff commit 367e315

File tree

102 files changed

+2660
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2660
-2
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ jobs:
196196
env:
197197
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
198198

199+
test-codemods:
200+
runs-on: ubuntu-latest
201+
name: Test
202+
steps:
203+
- name: Checkout
204+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
205+
206+
- name: Setup Node.js and deps
207+
uses: ./.github/actions/setup-deps
208+
209+
- name: Test
210+
run: yarn test:codemods
211+
199212
test-rn-0-83-1:
200213
runs-on: ubuntu-latest
201214
name: Test RN 0.83.1

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
.yarn
3+
codemods/**/tests/fixtures/**

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ npmPreapprovedPackages:
66
- react
77
- react-native
88
- universal-test-renderer
9+
- '@testing-library/react-native'
910
- '@react-native/*'
1011
- '@types/react'
11-
- '@types/universal-test-renderer'
1212
- hermes-compiler
1313

1414
yarnPath: .yarn/releases/yarn-4.11.0.cjs

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ This document provides context for the any code assistant to understand the `@te
1010
- **Tech Stack:** TypeScript, React Native, Jest.
1111
- **Architecture:** The library simulates the React Native runtime on top of `universal-test-renderer`.
1212

13+
## Project Guidelines
14+
15+
- Small API surface
16+
- Expose all features of the underlying platform (react, react-reconciler) for Testing Libraries to use
17+
- Render host elements only, yet provide escape hatches to fibers when needed
18+
1319
## Building and Running
1420

1521
The project uses `yarn` for dependency management and script execution.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Dependencies
2+
node_modules/
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
7+
# Build artifacts
8+
target/
9+
dist/
10+
build/
11+
12+
# Temporary files
13+
*.tmp
14+
*.temp
15+
.cache/
16+
17+
# Environment files
18+
.env
19+
.env.local
20+
21+
# IDE files
22+
.vscode/
23+
.idea/
24+
*.swp
25+
*.swo
26+
27+
# OS files
28+
.DS_Store
29+
Thumbs.db
30+
31+
# Package bundles
32+
*.tar.gz
33+
*.tgz
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
schema_version: '1.0'
2+
3+
name: 'rntl-v14-async-functions'
4+
version: '0.1.0'
5+
description: 'Codemod to migrate sync RNTL function and method calls to async for RNTL v14'
6+
author: 'Maciej Jastrzebski'
7+
license: 'MIT'
8+
workflow: 'workflow.yaml'
9+
10+
targets:
11+
languages: ['typescript', 'tsx', 'javascript', 'jsx']
12+
13+
keywords: ['transformation', 'migration']
14+
15+
registry:
16+
access: 'public'
17+
visibility: 'public'
18+
19+
capabilities: []
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@testing-library/react-native-v14-async-functions",
3+
"version": "0.1.0",
4+
"description": "Codemod to migrate render() calls to await render() for RNTL v14",
5+
"type": "module",
6+
"scripts": {
7+
"test": "yarn dlx codemod@latest jssg test -l tsx ./scripts/codemod.ts",
8+
"check-types": "tsc --noEmit"
9+
},
10+
"devDependencies": {
11+
"@codemod.com/jssg-types": "^1.3.0",
12+
"typescript": "^5.8.3"
13+
}
14+
}

0 commit comments

Comments
 (0)