Skip to content

Commit d36137b

Browse files
docs: troubleshooting undefined components (#1502)
1 parent f538029 commit d36137b

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

website/docs/Troubleshooting.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import TOCInline from '@theme/TOCInline';
77
This guide describes common issues found by users when integrating React Native Test Library to their projects:
88

99
<TOCInline toc={toc} />
10-
1110
## Matching React Native, React & React Test Renderer versions
1211

1312
Check that you have matching versions of core dependencies:
@@ -33,6 +32,45 @@ We maintain an [example repository](https://github.com/callstack/react-native-te
3332

3433
In case something does not work in your setup you can refer to this repository for recommended configuration.
3534

35+
## Undefined component error
36+
37+
> Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
38+
39+
This frequently happens when you mock a complex module incorrectly, e.g.:
40+
41+
```ts
42+
jest.mock('@react-navigation/native', () => {
43+
return {
44+
useNavigation: jest.fn(),
45+
};
46+
})
47+
```
48+
49+
The above mock will mock `useNavigation` hook as intended, but at the same time all other exports from `@react-navigation/native` package are now `undefined`. If you want to use `NavigationContainer` component from the same package it will be `undefined` and result in the error above.
50+
51+
In order to mock only a part of given package you should re-export all other exports using `jest.requireActual` helper:
52+
53+
```ts
54+
jest.mock('@react-navigation/native', () => {
55+
return {
56+
...jest.requireActual('@react-navigation/native'),
57+
useNavigation: jest.fn(),
58+
};
59+
})
60+
```
61+
62+
That way the mock will re-export all of the `@react-navigation/native` members and overwrite only the `useNavigation` hook.
63+
64+
Alternatively, you can use `jest.spyOn` to mock package exports selectively.
65+
66+
### Mocking React Native
67+
68+
In case of mocking `react-native` package you should not mock the whole package at once, as this approach has issues with `jest.requireActual` call. In this case it is recommended to mock particular library paths inside the package, e.g.:
69+
70+
```ts
71+
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
72+
```
73+
3674
## Act warnings
3775

3876
When writing tests you may encounter warnings connected with `act()` function. There are two kinds of these warnings:

0 commit comments

Comments
 (0)