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
Copy file name to clipboardExpand all lines: README-v13.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,11 +14,11 @@
14
14
15
15
## The problem
16
16
17
-
You want to write maintainable tests for your React Native components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your tests to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.
17
+
You want to write maintainable tests for your React Native components. Your tests should avoid implementation details and focus on giving you confidence. They should remain maintainable so refactors (changes to implementation but not functionality) don't break your tests and slow you and your team down.
18
18
19
19
## This solution
20
20
21
-
The React Native Testing Library (RNTL) is a comprehensive solution for testing React Native components. It provides React Native runtime simulation on top of `react-test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
21
+
The React Native Testing Library (RNTL) tests React Native components. It simulates the React Native runtime on top of `react-test-renderer` and encourages better testing practices. Its primary guiding principle is:
22
22
23
23
> The more your tests resemble the way your software is used, the more confidence they can give you.
24
24
@@ -48,8 +48,8 @@ You can use the built-in Jest matchers automatically by having any import from `
-[Jest matchers](https://callstack.github.io/react-native-testing-library/docs/api/jest-matchers) - validate assumptions about your UI
90
-
-[User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type) in a realistic way
91
-
-[Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event in a simplified way
90
+
-[User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type)
91
+
-[Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event
92
92
-[`renderHook` function](https://callstack.github.io/react-native-testing-library/docs/api/misc/render-hook) - render hooks for testing purposes
Copy file name to clipboardExpand all lines: README.md
+4-18Lines changed: 4 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,11 +19,11 @@
19
19
20
20
## The problem
21
21
22
-
You want to write maintainable tests for your React Native components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your tests to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.
22
+
You want to write maintainable tests for your React Native components. Your tests should avoid implementation details and focus on giving you confidence. They should remain maintainable so refactors (changes to implementation but not functionality) don't break your tests and slow you and your team down.
23
23
24
24
## This solution
25
25
26
-
The React Native Testing Library (RNTL) is a comprehensive solution for testing React Native components. It provides React Native runtime simulation on top of `test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
26
+
The React Native Testing Library (RNTL) tests React Native components. It simulates the React Native runtime on top of [Test Renderer](https://github.com/mdjastrzebski/test-renderer) and encourages better testing practices. Its primary guiding principle is:
27
27
28
28
> The more your tests resemble the way your software is used, the more confidence they can give you.
29
29
@@ -34,20 +34,12 @@ This project is inspired by [React Testing Library](https://github.com/testing-l
This library has a `peerDependencies` listing for [Test Renderer](https://github.com/mdjastrzebski/test-renderer). Make sure to install it as a dev dependency:
45
41
46
42
```sh
47
-
# Yarn install:
48
-
yarn add --dev test-renderer
49
-
50
-
# NPM install
51
43
npm install --save-dev test-renderer
52
44
```
53
45
@@ -61,10 +53,6 @@ You can use the built-in Jest matchers automatically by having any import from `
-[Jest matchers](https://callstack.github.io/react-native-testing-library/docs/api/jest-matchers) - validate assumptions about your UI
103
-
-[User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type) in a realistic way
104
-
-[Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event in a simplified way
91
+
-[User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type)
92
+
-[Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event
105
93
-[`renderHook` function](https://callstack.github.io/react-native-testing-library/docs/api/misc/render-hook) - render hooks for testing purposes
Copy file name to clipboardExpand all lines: website/docs/14.x/docs/advanced/testing-env.mdx
+13-15Lines changed: 13 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ This document is intended for a more advanced audience who want to understand th
6
6
7
7
:::
8
8
9
-
React Native Testing Library allows you to write integration and component tests for your React Native app or library. While the JSX code used in tests closely resembles your React Native app, things are not as simple as they might appear. This document will describe the key elements of our testing environment and highlight things to be aware of when writing more advanced tests or diagnosing issues.
9
+
React Native Testing Library lets you write integration and component tests for your React Native app or library. While the JSX code in tests closely resembles your React Native app, the underlying environment differs. This document describes the key elements of our testing environment and highlights things to be aware of when writing advanced tests or diagnosing issues.
10
10
11
11
## React renderers
12
12
@@ -20,21 +20,19 @@ When you run your tests in the React Native Testing Library, somewhat contrary t
20
20
21
21
## Test Renderer
22
22
23
-
Instead, RNTL uses [Test Renderer](https://github.com/mdjastrzebski/test-renderer), a modern, actively maintained renderer that allows rendering to pure JavaScript objects without access to mobile OS and can run in a Node.js environment using Jest (or any other JavaScript test runner). Test Renderer replaces the deprecated `react-test-renderer` package and provides better compatibility with React 19 and improved type safety.
23
+
Instead, RNTL uses [Test Renderer](https://github.com/mdjastrzebski/test-renderer), a modern, actively maintained renderer that renders to pure JavaScript objects without access to mobile OS and runs in a Node.js environment using Jest (or any other JavaScript test runner). Test Renderer replaces the deprecated `react-test-renderer` package and has better compatibility with React 19 and improved type safety.
24
24
25
-
Using Test Renderer has pros and cons.
25
+
Using Test Renderer has trade-offs:
26
26
27
27
Benefits:
28
-
29
-
- tests can run on most CIs (Linux, etc) and do not require a mobile device or emulator
30
-
- faster test execution
31
-
- light runtime environment
32
-
33
-
Disadvantages:
34
-
35
-
- Tests do not execute native code
36
-
- Tests are unaware of the view state that would be managed by native components, e.g., focus, unmanaged text boxes, etc.
37
-
- Assertions do not operate on native view hierarchy
28
+
- Tests run on most CIs (Linux, etc) without a mobile device or emulator
29
+
- Faster test execution
30
+
- Light runtime environment
31
+
32
+
Limitations:
33
+
- Tests don't execute native code
34
+
- Tests are unaware of view state managed by native components, e.g., focus, unmanaged text boxes, etc.
35
+
- Assertions don't operate on native view hierarchy
38
36
- Runtime behaviors are simulated, sometimes imperfectly
39
37
40
38
It's worth noting that the React Testing Library (web one) works a bit differently. While RTL also runs in Jest, it has access to a simulated browser DOM environment from the `jsdom` package, which allows it to use a regular React DOM renderer. Unfortunately, there is no similar React Native runtime environment package. This is probably because while the browser environment is well-defined and highly standardized, the React Native environment constantly evolves in sync with the evolution of underlying OS-es. Maintaining such an environment would require duplicating countless React Native behaviors and keeping them in sync as React Native develops.
@@ -90,13 +88,13 @@ Not all React Native components are organized this way, e.g., when you use `Pres
90
88
91
89
### Host-only element tree
92
90
93
-
In RNTL v14, [Test Renderer](https://github.com/mdjastrzebski/test-renderer) only exposes host elements in the element tree. Composite components are not visible in the tree - you only see their host element output. This is an intentional design choice that aligns with Testing Library's philosophy: tests should focus on what users can see and interact with (host elements), not on implementation details (composite components).
91
+
In RNTL v14, [Test Renderer](https://github.com/mdjastrzebski/test-renderer) only exposes host elements in the element tree. Composite components aren't visible in the tree—you only see their host element output. This aligns with Testing Library's philosophy: tests should focus on what users can see and interact with (host elements), not implementation details (composite components).
94
92
95
93
For a `HostElement`, the `type` prop is always a string value representing the host component name, e.g., `"View"`, `"Text"`, `"TextInput"`.
96
94
97
95
## Tree nodes
98
96
99
-
RNTL v14 queries and the element tree only expose host elements. This aligns with Testing Library's philosophy: tests should assert on what users can see and interact with. Host elements represent the actual UI controls that users interact with, while composite components exist purely in the JavaScript domain.
97
+
RNTL v14 queries and the element tree only expose host elements. Tests assert on what users can see and interact with. Host elements represent the actual UI controls users interact with, while composite components exist purely in the JavaScript domain.
Copy file name to clipboardExpand all lines: website/docs/14.x/docs/advanced/understanding-act.mdx
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Understanding `act` function
2
2
3
-
When writing RNTL tests one of the things that confuses developers the most are cryptic [`act()`](https://react.dev/link/wrap-tests-with-act) function errors logged into console. In this article I will try to build an understanding of the purpose and behaviour of `act()` so you can build your tests with more confidence.
3
+
When writing RNTL tests, cryptic [`act()`](https://react.dev/link/wrap-tests-with-act) function errors logged to console often confuse developers. This article explains the purpose and behavior of `act()` so you can write tests with more confidence.
4
4
5
5
## `act` warning
6
6
@@ -28,7 +28,7 @@ This function is intended only for using in automated tests and works only in de
28
28
29
29
The responsibility for `act` function is to make React renders and updates work in tests in a similar way they work in real application by grouping and executing related units of interaction (e.g. renders, effects, etc) together.
30
30
31
-
To showcase that behaviour let make a small experiment. First we define a function component that uses `useEffect` hook in a trivial way.
31
+
Let's demonstrate this with a small experiment. First, define a function component that uses `useEffect`:
32
32
33
33
```jsx
34
34
functionTestComponent() {
@@ -57,7 +57,7 @@ test('render without act', () => {
57
57
});
58
58
```
59
59
60
-
When testing without `act`call wrapping rendering call, we see that the assertion runs just after the rendering but before `useEffect`hooks effects are applied. Which is not what we expected in our tests.
60
+
When testing without `act` wrapping the render call, the assertion runs just after rendering but before `useEffect` effects are applied. This isn't what we expected.
61
61
62
62
```jsx
63
63
import { createRoot } from'test-renderer';
@@ -99,7 +99,7 @@ Note that `act` calls can be safely nested and internally form a stack of calls.
99
99
100
100
The `act` implementation is defined in the [ReactAct.js source file](https://github.com/facebook/react/blob/main/packages/react/src/ReactAct.js) inside React repository. RNTL v14 requires React 19+, which provides the `act` function directly via `React.act`.
101
101
102
-
RNTL exports `act` for convenience of the users as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). In v14, `act` is now async by default and always returns a Promise, making it compatible with async React features like `Suspense`boundary or`use()` hook. The underlying implementation wraps React's `act` function to ensure consistent async behavior.
102
+
RNTL exports `act` for convenience as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). In v14, `act` is async by default and always returns a Promise. This works with async React features like `Suspense`boundaries and the`use()` hook. The underlying implementation wraps React's `act` function to ensure consistent async behavior.
103
103
104
104
**Important**: You should always use `act` exported from `@testing-library/react-native` rather than the one from `react`. The RNTL version automatically ensures async behavior, whereas using `React.act` directly could still trigger synchronous act behavior if used improperly, leading to subtle test issues.
105
105
@@ -111,7 +111,7 @@ In v14, `act` is always async and returns a Promise. While the callback you pass
111
111
112
112
When the callback passed to `act` contains asynchronous operations, the Promise returned by `act` will resolve only after those operations complete.
113
113
114
-
Lets look at a simple example with component using `setTimeout`call to simulate asynchronous behaviour:
114
+
Here's a simple example with a component using `setTimeout` to simulate asynchronous behavior:
115
115
116
116
```jsx
117
117
functionTestAsyncComponent() {
@@ -139,7 +139,7 @@ If we test our component in a native way without handling its asynchronous behav
139
139
140
140
### Solution with fake timers
141
141
142
-
First solution is to use Jest's fake timers inside out tests:
142
+
Use Jest's fake timers:
143
143
144
144
```jsx
145
145
test('render with fake timers', async () => {
@@ -159,7 +159,7 @@ That way we can wrap `jest.runAllTimers()` call which triggers the `setTimeout`
159
159
160
160
### Solution with real timers
161
161
162
-
If we wanted to stick with real timers then things get a bit more complex. Let's start by applying a crude solution of opening async `act()` call for the expected duration of components updates:
162
+
With real timers, things get more complex. Start with a simple solution: wrap an async `act()` call for the expected duration of component updates:
163
163
164
164
```jsx
165
165
test('render with real timers - sleep', async () => {
@@ -174,7 +174,7 @@ test('render with real timers - sleep', async () => {
174
174
175
175
This works correctly as we use an explicit async `act()` call that resolves the console error. However, it relies on our knowledge of exact implementation details which is a bad practice.
176
176
177
-
Let's try more elegant solution using`waitFor`that will wait for our desired state:
177
+
A better solution uses`waitFor`to wait for the desired state:
178
178
179
179
```jsx
180
180
test('render with real timers - waitFor', async () => {
0 commit comments