|
| 1 | +--- |
| 2 | +id: testing-env |
| 3 | +title: Testing Environment |
| 4 | +--- |
| 5 | + |
| 6 | +:::info |
| 7 | +This document is intended for more advanced audience. You should be able to write integration or component tests without reading this. |
| 8 | +It is intended for people who want to better understand internals of our testing environment, e.g. in order to contribute to the codebase. |
| 9 | +::: |
| 10 | + |
| 11 | +## Testing Environment |
| 12 | + |
| 13 | +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, the things are not quite as simple as they might appear. In this document we will describe the key elements of our testing environment and highlight things to be aware of when writing more advanced tests or diagnosing issues. |
| 14 | + |
| 15 | +### React renderers |
| 16 | + |
| 17 | +React allows you to write declarative code using JSX, write function or class components, or use hooks like `useState`. In order to output the results of your components it needs to work with a renderer. Every React app uses some type of renderer: React Native is a renderer for mobile apps, web apps use React DOM, and there are other more [specialised renderers](https://github.com/chentsulin/awesome-react-renderer) that can e.g. render to console or HTML canvas. |
| 18 | + |
| 19 | +When you run your tests in React Native Testing Library, somewhat contrary to what the name suggest, they are actually **not** using React Native renderer. This is because this renderer needs to be run on iOS or Android operating system, so it would need to run on device or simulator. |
| 20 | + |
| 21 | +### React Test Renderer |
| 22 | + |
| 23 | +Instead, RNTL uses React Test Renderer which is a specialised renderer that allows rendering to pure JavaScript objects without access to mobile OS, and that can run in a Node.js environment using Jest (or any other JavaScript test runner). |
| 24 | + |
| 25 | +Using React Test Renderer has pros and cons. |
| 26 | + |
| 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 not aware of 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 |
| 38 | +- Runtime behaviours are simulated, sometimes imperfectly |
| 39 | + |
| 40 | +It’s worth noting that React Testing Library (web one), works a bit different. While RTL also runs in Jest, it also has access to simulated browser DOM environment from jsdom package, so it can use a regular React DOM renderer. Unfortunately, there is no similar React Native runtime environment package. This is probably due to to the fact that while browser environment is well defined and highly standardised, the React Native environment is in constant evolution, in sync with the evolution of underlying OS-es. Maintaining such environment would require duplicating countless React Native behaviours, and keeping that in sync as React Native evolves. |
| 41 | + |
| 42 | +### Element tree |
| 43 | + |
| 44 | +Invoking `render()` function results in creation of an element tree. This is done internally by invoking `TestRenderer.create()` method. The output tree represents your React Native component tree, each node of that tree is an “instance” of some React component (to be more precise: each node represents a React fiber, and only class components have instances, while function components store the hook state using fiber). |
| 45 | + |
| 46 | +These tree elements are represented by `ReactTestInstance` type: |
| 47 | + |
| 48 | +```tsx |
| 49 | +interface ReactTestInstance { |
| 50 | + type: ElementType; |
| 51 | + props: { [propName: string]: any }; |
| 52 | + parent: ReactTestInstance | null; |
| 53 | + children: Array<ReactTestInstance | string>; |
| 54 | + |
| 55 | + // Other props and methods |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Based on: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-test-renderer/index.d.ts |
| 60 | + |
| 61 | +### Host and composite components |
| 62 | + |
| 63 | +One of the most important aspects of the element tree is that it is composed of both host and composite components: |
| 64 | + |
| 65 | +- [Host components](https://reactnative.dev/architecture/glossary#react-host-components-or-host-components) are components that will have direct counterparts in the native view tree. Typical examples are `<View>`, `<Text>` , `<TextInput>`, and `<Image>` from React Native. You can think of these as analogue of `<div>`, `<span>` etc on the Web. You can also create your own host views as native modules or import them from 3rd party libraries, like React Navigation or React Native Gesture Handler. |
| 66 | +- [Composite components](https://reactnative.dev/architecture/glossary#react-composite-components) are React code organisation units that exist only on the JavaScript side of your app. Typical examples are components you create (both function and class components), components imported from React Native (`View`, `Text`, etc) or from 3rd party packages. |
| 67 | + |
| 68 | +That might sound a bit confusing at first, since we put React Native’s `View` in both categories. There are actually two `View` components: composite one and host one. The relation between them is as follows: |
| 69 | + |
| 70 | +- composite `View` is the type imported from `react-native` package. It’s a JavaScript component, which renders host `View` as its only child in the element tree. |
| 71 | +- host `View` , which you do not render directly. React Native takes the props you pass to the composite `View`, does some processing on them and passes them to host `View`. |
| 72 | + |
| 73 | +The part of the tree looks as follows: |
| 74 | + |
| 75 | +```jsx |
| 76 | +* <View> (composite) |
| 77 | + * <View> (host) |
| 78 | + * children prop passed in JSX |
| 79 | +``` |
| 80 | + |
| 81 | +Similar relation exists between other composite and host pairs: e.g. `Text` , `TextInput` and `Image` components: |
| 82 | + |
| 83 | +```jsx |
| 84 | +* <Text> (composite) |
| 85 | + * <Text> (host) |
| 86 | + * string (or mixed) content |
| 87 | +``` |
| 88 | + |
| 89 | +Not all React Native components are organised this way, e.g. when you use `Pressable` (or `TouchableOpacity`) there is no host `Pressable`, but composite `Pressable` is rendering a host `View` with certain props being set: |
| 90 | + |
| 91 | +```jsx |
| 92 | +* <Pressable> (composite) |
| 93 | + * <View accessible={true} {...}> (host) |
| 94 | + * children prop passed in JSX |
| 95 | +``` |
| 96 | + |
| 97 | +#### Differentiating between host and composite elements |
| 98 | + |
| 99 | +Any easy way to differentiate between host and composite elements is the `type` prop of `ReactTestInstance`: |
| 100 | + |
| 101 | +- for host components it’s always a string value representing component name, e.g. `"View"` |
| 102 | +- for composite components it’s a function or class corresponding to the component |
| 103 | + |
| 104 | +You can use the following code to check if given element is a host one: |
| 105 | + |
| 106 | +```jsx |
| 107 | +function isHostElement(element: ReactTestInstance) { |
| 108 | + return typeof element.type === 'string'; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Tree nodes |
| 113 | + |
| 114 | +We encourage you to only assert values on host views in your tests, because they represent the user interface view and controls that the user will be able to see and interact with. Users cannot see, or interact with, composite views as they exist purely in JavaScript domain and do not generate any visible UI. |
| 115 | + |
| 116 | +#### Asserting props |
| 117 | + |
| 118 | +As an example, if you make assertions on a `style` prop of a composite element, there is no guarantee that the style will be visible to the user, as the component author can forget to pass this prop to some underlying `View` or other host component. Similarly `onPress` event handler on a composite prop can be unreachable by the user. |
| 119 | + |
| 120 | +```jsx |
| 121 | +function ForgotToPassPropsButton({ title, onPress, style }) { |
| 122 | + return ( |
| 123 | + <Pressable> |
| 124 | + <Text>{title}</Text> |
| 125 | + </Pressable> |
| 126 | + ); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +In the above example user defined components accepts both `onPress` and `style` props but does not pass it (through `Pressable`) to host views, so these props will not affect the user interface. Additionally, React Native and other libraries might pass some of the props under different names or transform their values between composite and host components. |
| 131 | + |
| 132 | +### Tree navigation |
| 133 | + |
| 134 | +:::caution |
| 135 | +You should avoid navigating over element tree, as this makes your testing code fragile and may result in false positives. This section is more relevant for people how want to contribute to our codebase. |
| 136 | +::: |
| 137 | + |
| 138 | +When navigating a tree of react elements using `parent` or `children` props of a `ReactTestInstance` element, you will encounter both host and composite elements. You should be careful when navigating the element tree, as the tree structure for 3rd party components and change independently from your code and cause unexpected test failures. |
| 139 | + |
| 140 | +Inside RNTL we have various tree navigation helpers: `getHostParent`, `getHostChildren`, etc. These are intentionally not exported as using them is not a recommended practice. |
| 141 | + |
| 142 | +### Queries |
| 143 | + |
| 144 | +Most of the Testing Library queries return host components, in order to encourage best practices described above. |
| 145 | + |
| 146 | +At this stage, there are some noteworthy exceptions: |
| 147 | + |
| 148 | +- `*ByText` queries returns composite `Text` element |
| 149 | +- `*ByDisplayValue` queries returns composite `TextInput` element |
| 150 | +- `*ByPlaceholderText` queries returns composite `TextInput` element |
| 151 | + |
| 152 | +This will change in the near future, as we make efforts for all queries to return host components. Meanwhile it shouldn't be a huge issue, as composite `Text` and `TextInput`generally pass their props down to host counterparts. |
| 153 | + |
| 154 | +Additionally, `UNSAFE_*ByType` and `UNSAFE_*ByProps` queries can return both host and composite components depending on used predicates. They are marked as unsafe precisely because testing composite components makes your test more fragile. |
0 commit comments