Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'NativeAnimatedHelper' when using Jest mock

I have been trying to setup react-navigation in my new React Native app. While I have the code/screens working, Jest is causing me issues after adding in the library by following the v4 docs.

I installed react-navigation, react-native-reanimated, react-native-gesture-handler, and react-native-screens@^1.0.0-alpha.23. I then went into the ios folder and ran pod install. Because I am on v0.61 of React Native, they should have been autolinked.

When I run the default Jest test, I get an error like:

  ✓ renders correctly (106ms)

  console.warn node_modules/react-native/Libraries/Animated/src/NativeAnimatedHelper.js:270
    Animated: `useNativeDriver` is not supported because the native animated module is missing. Falling back to JS-based animation. To resolve this, add `RCTAnimation` module to this app, or remove `useNativeDriver`. More info: https://github.com/facebook/react-native/issues/11094#issuecomment-263240420

I searched for this error, and most of the suggested answers were to put this in my test file:

jest.mock('NativeAnimatedHelper');

However, when I tried doing so, Jest complaing with an error like so:

 FAIL  __tests__/App-test.tsx
  ● Test suite failed to run

    Cannot find module 'NativeAnimatedHelper' from 'App-test.tsx'

       6 | import renderer from 'react-test-renderer'
       7 |
    >  8 | jest.mock('NativeAnimatedHelper')
         | ^
       9 |
      10 | it('renders correctly', () => {
      11 |   renderer.create(<App />)

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
      at Object.<anonymous> (__tests__/App-test.tsx:8:1)

I've been trying various things for awhile now, any help/suggestions would be appreciated!


Versions:

Node: 10.16.1
npm: 6.11.3
"react": "^16.9.0",
"react-native": "0.61.1",
"react-native-gesture-handler": "^1.4.1",
"react-native-reanimated": "^1.3.0",
"react-native-screens": "^1.0.0-alpha.23",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^1.9.3",

"babel-jest": "^24.1.0"
"jest": "^24.1.0"
"react-test-renderer": "^16.9.0"

Jest config:

{
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "setupFiles": [
      "./node_modules/react-native-gesture-handler/jestSetup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|react-navigation|@react-navigation/.*))"
    ]
  }
}
like image 616
Saad Avatar asked Oct 06 '19 13:10

Saad


Video Answer


3 Answers

Current way of fixing this issue

Updated May 18, 2021

It looks like the location of NativeAnimatedHelper has moved in a more recent version of React Native.

Change

jest.mock('NativeAnimatedHelper');

To

jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');


Previous way of fixing this issue

Change

jest.mock('NativeAnimatedHelper');

To

jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

like image 155
João Gusmão Avatar answered Oct 26 '22 02:10

João Gusmão


React Native v0.64.0

The src folder was removed from Animated

You need to remove src from the mock path:

Before:
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

After:
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
like image 24
artiom-damaschin Avatar answered Oct 26 '22 04:10

artiom-damaschin


With RN 64, it should be jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');

like image 20
ethanneff Avatar answered Oct 26 '22 04:10

ethanneff