You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`getByRole` is the most accessible query and should be your first choice. It queries elements by their semantic role:
56
56
@@ -88,11 +88,11 @@ Common roles in React Native include:
88
88
-`radio` - radio buttons
89
89
- And more...
90
90
91
-
Note: React Native supports both ARIA-compatible (`role`) and traditional (`accessibilityRole`) props. Prefer `role` for consistency with web standards.
91
+
Note: React Native supports both ARIA-compatible (`role`) and legacy (`accessibilityRole`) props. Prefer `role` for consistency with web standards.
92
92
93
93
## Using the wrong assertion {#using-the-wrong-assertion}
React Native doesn't have a DOM, so there's no `container.querySelector()`. Instead, React Native Testing Library provides a `container` object that has a `queryAll` method, but you should avoid using it directly:
236
+
React Native Testing Library provides a `container` object that has a `queryAll` method, but you should avoid using it directly:
273
237
274
238
```tsx
275
239
import { View, Text } from'react-native';
@@ -294,7 +258,7 @@ Instead, use the proper query methods from `screen` or the `render` result. The
294
258
295
259
## Passing an empty callback to `waitFor`{#passing-an-empty-callback-to-waitfor}
React Native Testing Library's `render`, `renderHook`, `userEvent`, and `fireEvent` are already wrapped in `act`, so you don't need to wrap them yourself:
There's an ESLint plugin for Testing Library: [`eslint-plugin-testing-library`](https://github.com/testing-library/eslint-plugin-testing-library). This plugin can help you avoid common mistakes and will automatically fix your code in many cases.
477
441
@@ -481,19 +445,21 @@ You can install it with:
481
445
yarn add --dev eslint-plugin-testing-library
482
446
```
483
447
484
-
And configure it in your ESLint config:
448
+
And configure it in your `eslint.config.js` (flat config):
React Native Testing Library automatically cleans up after each test. You don't need to call `cleanup()` manually unless you're using the `pure` export (which doesn't include automatic cleanup).
`getBy*` queries throw errors when elements aren't found, so they work as assertions. However, for better error messages, you might want to combine them with explicit matchers:
This is not really a "mistake" per se, but it's a common pattern that can be improved. When you use the `wrapper` option in `render`, you might be tempted to name your wrapper component `Wrapper`:
592
558
@@ -620,6 +586,4 @@ The key principles to remember:
620
586
5.**Follow accessibility best practices** - Prefer ARIA attributes (`role`, `aria-label`) over `accessibility*` props
621
587
6.**Organize code well** - Use `screen` over destructuring, prefer `userEvent` over `fireEvent`, and don't use `cleanup()`
622
588
623
-
For detailed guidelines and examples, see the [LLM Guidelines](llm-guidelines) document.
624
-
625
589
By following these principles, your tests will be more maintainable, accessible, and reliable.
0 commit comments