Importing react-native-gesture-handler crashes within jest
See original GitHub issueI just upgraded to react-navigation@3.0 which now makes me a consumer of react-native-gesture-handler! Wooo!
Unfortunately, when I run our jest snapshot tests, the global scope of react-native-gesture-handler expects certain native modules to exist!
This means that jest then crashes in any tests that import react-navigation which imports react-native-gesture-handler.
Workaround:
NativeModules.UIManager = NativeModules.UIManager || {};
NativeModules.UIManager.RCTView = NativeModules.UIManager.RCTView || {};
NativeModules.RNGestureHandlerModule = NativeModules.RNGestureHandlerModule || {
State: { BEGAN: "BEGAN", FAILED: "FAILED", ACTIVE: "ACTIVE", END: "END" },
};
I embedded the above code near the top of file in my node_modules/react-native-gesture-handler/GestureHandler.js
, and the problem went away. – For the medium term, I’ll put the following snippet in my jest beforeAll.js
:
import { NativeModules as RNNativeModules } from "react-native";
RNNativeModules.UIManager = RNNativeModules.UIManager || {};
RNNativeModules.UIManager.RCTView = RNNativeModules.UIManager.RCTView || {};
RNNativeModules.RNGestureHandlerModule = RNNativeModules.RNGestureHandlerModule || {
State: { BEGAN: "BEGAN", FAILED: "FAILED", ACTIVE: "ACTIVE", END: "END" },
};
I’m happy to help contribute to this repo, but I don’t know what strategy you want to take when solving this.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:15
- Comments:72 (7 by maintainers)
Top Results From Across the Web
Testing with Jest | React Native Gesture Handler
Testing with Jest. Mocking native modules. In order to load mocks provided by RNGH add following to your jest config in package.json :...
Read more >GestureDetector gesture handler app crash when calling ...
The reanimated, gesture handler hooks and callbacks works on the UI thread and the trigger function you defined is by default on the...
Read more >Testing React Native with Jest - Laurence's Blog
The fix for this is to add this "seutpFiles" code to the "jest" object in package.json (see: Importing react-native-gesture-handler crashes ...
Read more >react-native-gesture-handler - npm
React Native Gesture Handler provides native-driven gesture management APIs for building best possible touch-based experiences in React ...
Read more >a problem occurred evaluating project ':react-native-gesture ...
1º Click in react-native-gesture-handler project on android studio 2º Click in Refactor on top ... and repeat the same with any other problem...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Still have to use thejest.setup.js
posted by @AndreasEK above on newest RNGH 1.2.1 for jest to work. GettingTypeError: Cannot read property 'Direction' of undefined
otherwise. The one liner by @voxspox above, by itself, didn’t work in my case.Actually, the following works:
Pretty hidden in the documentation, found it by looking at the commits. See https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#testing
I managed to fix this issue for me by creating a setup file called
jestSetup.js
with the following content:Which is a combination of the workaround posted by others in this issue.
I then added
to the jest configuration section in
package.json
.