react-native-reanimated: [Jest] V2 - rc2 Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'NativeReanimated'

Description

I bumped RC 1 -> RC 2 in my project and suffered JEST error like the following

 ● Test suite failed to run

    Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'NativeReanimated' could not be found. Verify that a module by this name is registered in the native binary.

      at invariant (node_modules/invariant/invariant.js:40:15)
      at Object.getEnforcing (node_modules/react-native/Libraries/TurboModule/TurboModuleRegistry.js:39:3)
      at Object.<anonymous> (node_modules/react-native-reanimated/src/reanimated2/NativeReanimated.native.js:5:23)
      at Object.<anonymous> (node_modules/react-native-reanimated/src/reanimated2/WorkletEventHandler.js:1:1)

My mock file is like that.

// @ts-ignore https://github.com/wcandillon/react-native-redash/issues/395
global.__reanimatedWorkletInit = () => {};

jest
  .mock('react-native-reanimated', () => ({
    ...require('react-native-reanimated/mock'),
  }))
  .mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

Expected behavior

Test should success

Package versions

  • React: 16.13.1
  • React Native: 0.63.4
  • React Native Reanimated: 2.0.0-rc02
  • NodeJS: 14.7.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 25
  • Comments: 18 (4 by maintainers)

Most upvoted comments

I opened a PR, but if you guys want a temporary fix. Use ds300/patch-package and put this on patches/react-native-reanimated+2.0.0-rc.2.patch:

diff --git a/node_modules/react-native-reanimated/mock.js b/node_modules/react-native-reanimated/mock.js
index 3bc10c5..c26bce3 100644
--- a/node_modules/react-native-reanimated/mock.js
+++ b/node_modules/react-native-reanimated/mock.js
@@ -234,6 +234,7 @@ module.exports = {
   __esModule: true,
 
   ...Reanimated,
+  ...ReanimatedV2,
 
   default: {
     ...Reanimated,
diff --git a/node_modules/react-native-reanimated/src/reanimated2/mock.js b/node_modules/react-native-reanimated/src/reanimated2/mock.js
index e4b6a8c..7b4ebe7 100644
--- a/node_modules/react-native-reanimated/src/reanimated2/mock.js
+++ b/node_modules/react-native-reanimated/src/reanimated2/mock.js
@@ -1,11 +1,18 @@
-const hooks = require('./Hooks');
 
 /* eslint-disable standard/no-callback-literal */
+const useRef = require('react').useRef
 const NOOP = () => {};
 const ID = (t) => t;
 
 const ReanimatedV2 = {
-  useSharedValue: hooks.useSharedValue,
+  useSharedValue: (init) => {
+    const ref = useRef(null);
+    if (ref.current === null) {
+      ref.current = { value: init };
+    }
+  
+    return ref.current
+  },
   useDerivedValue: (a) => ({ value: a() }),
   useAnimatedScrollHandler: () => NOOP,
   useAnimatedGestureHandler: () => NOOP,

This is not only Jest related. Same happens when

  • remote debugging
  • with rc-1 as well

Remote debugging issue indicates me Hermes comes into play. That is not viable on iOS for me 🤷‍♂️

I can also confirm that changing my project from 2.0.0-rc2 to 2.0.0-rc1 stopped jest from failing. Would really like RC2 to work as the withTiming caused crashes on ios in this version…

Thanks

On Thu, 18 Mar 2021, 12:04 pm p2mittal-dev, @.***> wrote:

Error - TurboModuleRegistry.getEnforcing(…): ‘NativeReanimated’ Cause - React Native Reanimated 2.0

Hello, I got this[TurboModuleRegistry.getEnforcing(…): ‘NativeReanimated’] error and i found the solution Step 1 - Download react native project from github " https://github.com/software-mansion-labs/reanimated-2-playground". Step 2- Then run command => npm install => react-native start => react-native run-android and build app Step 3- when its work move your required project files and install your required npm pakages. Step 4 - Then run commands => npm cache clean --force => cd android => gradlew clean => gradle cleanBuildCache => gradlew build --refresh-dependencies => cd… => npx react-native start --reset-cache => npx react-native run-android its work for me try once.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/software-mansion/react-native-reanimated/issues/1642#issuecomment-801670187, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALO4WP44PYTY6ZS67ZG3QALTEGNIDANCNFSM4WFZOS5A .

Error Solved

Error - TurboModuleRegistry.getEnforcing(…): ‘NativeReanimated’ Hello, I got this[TurboModuleRegistry.getEnforcing(…): ‘NativeReanimated’] error and i found the solution Step 1 - Download react native project from github “https://github.com/software-mansion-labs/reanimated-2-playground”. Step 2- Then run command => npm install => react-native start => react-native run-android and build app Step 3- when its work move your required project files and install your required npm pakages. Step 4 - Then run commands => npm cache clean --force => cd android => gradlew clean => gradle cleanBuildCache => gradlew build --refresh-dependencies => cd… => npx react-native start --reset-cache => npx react-native run-android its work for me try once.

This fails again on rc.3 unfortunately.