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: website/docs/MigrationV2.md
+36-35Lines changed: 36 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -3,75 +3,72 @@ id: migration-v2
3
3
title: Migration to 2.0
4
4
---
5
5
6
-
This guides describes major steps involved in migrating your testing code from using React Native Testing Library version `1.x` to version `2.0`.
6
+
This guide describes steps necessary to migrate from React Native Testing Library `v1.x` to `v2.0`.
7
7
8
8
## Dropping Node 8
9
9
10
-
Node 8 reached its EOL more than 5 months ago, so it's about time to target the library to Node 10. If you used lower version, you'll have to upgrade to v10, but we suggest using the latest LTS version.
10
+
Node 8 reached its EOL more than 5 months ago, so it's about time to target the library to Node 10. If you used lower version, you'll have to upgrade to v10, but we recommend using the latest LTS version.
11
11
12
12
## Auto Cleanup
13
13
14
-
`cleanup()` function is now called automatically after every test, if your testing framework supports `afterEach` hook (like Jest, mocha, and Jasmine).
14
+
`cleanup()` function is now called automatically after every test if your testing framework supports `afterEach` hook (like Jest, Mocha, and Jasmine).
15
15
16
-
You should be able to safely remove all `afterEach(cleanup)` calls in your code.
16
+
You should be able to remove all `afterEach(cleanup)` calls in your code.
17
17
18
-
This change might break your code, if you tests are not isolated, i.e. you call `render` outside `test` block. Generally, you should [keep your tests isolated](https://kentcdodds.com/blog/test-isolation-with-react), but if you can't or don't want to do this right away you can prevent this behavior using any of the following ways:
18
+
This change might break your code, if you tests are not isolated, i.e. you call `render` outside `test` block. Generally, you should [keep your tests isolated](https://kentcdodds.com/blog/test-isolation-with-react). But if you can't or don't want to do this right away you can prevent this behavior using any of the following ways:
19
19
20
-
1. by importing `'react-native-testing-library/pure'` instead of `'react-native-testing-library'`
20
+
- by importing `'react-native-testing-library/pure'` instead of `'react-native-testing-library'`
21
+
- by importing `'react-native-testing-library/dont-cleanup-after-each'` before importing `'react-native-testing-library'`. You can do it in a global way by using Jest's `setupFiles` like this:
21
22
22
-
2. by importing `'react-native-testing-library/dont-cleanup-after-each'` before importing `'react-native-testing-library'`. You can do it in a global way by using Jest's `setupFiles` like this:
3. by setting `RTNL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this:
29
+
- by setting `RTNL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this:
31
30
32
-
```sh
33
-
cross-env RNTL_SKIP_AUTO_CLEANUP=true jest
34
-
```
31
+
```sh
32
+
cross-env RNTL_SKIP_AUTO_CLEANUP=true jest
33
+
```
35
34
36
35
## WaitFor API changes
37
36
38
-
`waitForElement` function has been renamed to `waitFor` for consistency with React Testing Library. Additionally the signature has slightly changed from:
37
+
We renamed `waitForElement` function to `waitFor` for consistency with React Testing Library. Additionally, the signature has slightly changed from:
39
38
40
39
```jsx
41
40
exportdefaultfunction waitForElement<T>(
42
41
expectation: () =>T,
43
42
timeout?:number,
44
43
interval?:number
45
-
:Promise<T> {
44
+
): Promise<T> {}
46
45
```
47
46
48
47
to:
49
48
50
49
```jsx
51
50
exportdefaultfunction waitFor<T>(
52
51
expectation: () =>T,
53
-
{
52
+
options:{
54
53
timeout?: number,
55
-
interval?: number
54
+
interval?: number,
56
55
}
57
-
): Promise<T> {
56
+
): Promise<T> {}
58
57
```
59
58
60
59
Both changes should improve code readibility.
61
60
62
-
:::note
63
-
Please note that in many cases `waitFor`call can be replaced by proper use of `findBy`asynchonous queries resulting in more streamlined test code.
61
+
:::tip
62
+
You can usually avoid `waitFor` by a proper use of `findBy`asynchronous queries. It will result in more streamlined testing experience.
64
63
:::
65
64
66
65
## Removed global `debug` function
67
66
68
-
Global`debug()`function has been removed in favor of `debug()`method returned from `render()` function.
67
+
The`debug()` method returned from `render()` function is all you need. We removed the global export to avoid confusion.
69
68
70
69
## Removed global `shallow` function
71
70
72
-
Global `shallow()` functions which has been previously deprecated has been removed.
73
-
74
-
Shallow rendering React component is usually not a good idea, so we decided to remove the API. However, if you find it useful or need to support legacy tests, feel free to implement it yourself. Here's a sample implementation:
71
+
Shallow rendering React component is usually not a good idea, so we decided to remove the API. But, if you find it useful or need to support legacy tests, feel free to use this implementation:
@@ -93,7 +90,7 @@ Following query functions have been removed after being deprecated for more than
93
90
-`queryByName`
94
91
-`queryAllByName`
95
92
96
-
The `*ByType` and `*ByProps` queries has been prefixed with `UNSAFE_`. You can safely rename them using global search/replace in your project:
93
+
The `*ByType` and `*ByProps` queries has been prefixed with `UNSAFE_`. You can rename them using global search/replace in your project:
97
94
98
95
-`getByType` -> `UNSAFE_getByType`
99
96
-`getAllByType` -> `UNSAFE_getAllByType`
@@ -104,21 +101,25 @@ The `*ByType` and `*ByProps` queries has been prefixed with `UNSAFE_`. You can s
104
101
-`queryByProps` -> `UNSAFE_queryByProps`
105
102
-`queryAllByProps` -> `UNSAFE_queryAllByProps`
106
103
107
-
## Some `byTestId` queries behavior changes
104
+
## Some `ByTestId` queries behavior changes
105
+
106
+
In version `1.x` the `getByTestId` and `queryByTestId` queries could return non-native tinstances. This was a serious bug. Other query functions like `getAllByTestId`, `queryAllByTestId`, `findByTestId` and `findAllByTestId` didn't have this issue. These correctly returned only native components instances (e.g. `View`, `Text`, etc) that got the `testID`.
108
107
109
-
In version `1.x``getByTestId` and `queryByTestId` could return non-native elements in tests. This was in contrast with other query functions: `getAllByTestId`, `queryAllByTestId`, `findByTestId` and `findAllByTestId` which returned only elements that would be rendered to native components (e.g. `View`, `Text`, etc).
108
+
In v2 we fixed this inconsistency, which may result in failing tests, if you relied on this behavior. There are few ways to handle these failures:
110
109
111
-
If you relied on setting `testID` for your custom components, you should probably set them on the root element of the returned JSX.
110
+
- pass the `testID` prop down so it can reach a native component, like `View` or `Text`
111
+
- replace `testID` with proper `accessibilityHint` or `accessibilityLabel` if it benefits the user
112
+
- use safe queries like `*ByText` or `*ByA11yHint`
112
113
113
114
:::caution
114
-
In general, you should avoid `byTestId` queries when possible and rather use queries that check things that can been seen by the user (e.g. `byText`, `byPlaceholder`, etc) or accessability queries (e.g. `byA11yHint`, `byA11yLabel`, etc).
115
+
In general, you should avoid `*byTestId` queries when possible. Use queries that check things that the user can interact with. Like `*ByText` or `*ByPlaceholder`or accessibility queries (e.g. `*ByA11yHint`, `*ByA11yLabel`).
115
116
:::
116
117
117
118
## Deprecated `flushMicrotasksQueue`
118
119
119
-
We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major version, as currently there are better alternatives available for helping you write async tests:`findBy` async queries and `waitFor` helper.
120
+
We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major. We have better alternatives available for helping you write async tests –`findBy` async queries and `waitFor` helper.
120
121
121
-
If you can't or don't want to migrate your tests, you can get rid of the deprecation warning by copy-pasting function's implementation into your project:
122
+
If you can't or don't want to migrate your tests, don't worry. You can use the same implementation we have today:
0 commit comments