question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing react-native-gesture-handler crashes within jest

See original GitHub issue

I 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!

https://github.com/kmagiera/react-native-gesture-handler/blob/77491194049b82f26f358a09abc662ef27265527/GestureHandler.js#L26-L56

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:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:72 (7 by maintainers)

github_iconTop GitHub Comments

72reactions
andreialecucommented, May 6, 2019

Still have to use the jest.setup.js posted by @AndreasEK above on newest RNGH 1.2.1 for jest to work. Getting TypeError: 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:

  setupFiles: [
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ]

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

62reactions
Lythenascommented, Jan 2, 2019

I managed to fix this issue for me by creating a setup file called jestSetup.js with the following content:

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" },
  attachGestureHandler: jest.fn(),
  createGestureHandler: jest.fn(),
  dropGestureHandler: jest.fn(),
  updateGestureHandler: jest.fn(),

};
RNNativeModules.PlatformConstants = RNNativeModules.PlatformConstants || {
  forceTouchAvailable: false
};

Which is a combination of the workaround posted by others in this issue.

I then added

"setupFiles": [
  "./jestSetup.js"
]

to the jest configuration section in package.json.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found