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.

jest will fail with react-native-reanimated

See original GitHub issue

Description

I created a react-native-library with react-native-reanimated. Now I’m trying to add tests to my project, but all the tests fail. It complains that Jest encountered an unexpected token. the full error report is below:

    ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/damon/Documents/thundersdata/frontend/rn-template/node_modules/react-native-reanimated/mock.js:71
          return <Component {...this.props} />;
                 ^

    SyntaxError: Unexpected token '<'

      2 | jest.mock('react-native-reanimated', () => {
      3 |   // eslint-disable-next-line @typescript-eslint/no-var-requires
    > 4 |   const Reanimated = require('react-native-reanimated/mock');
        |                      ^
      5 | 
      6 |   // The mock for `call` immediately calls the callback which is incorrect
      7 |   // So we override it with a no-op

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at jest/setup.js:4:22
      at Object.<anonymous> (node_modules/@td-design/react-native/src/accordion/Panel.tsx:3:1)

Screenshots

image

steps to reproduce

Expected behavior

I should run all the tests successfully.

Actual behavior

it fails.

Snack or minimal code example

you can clone my project here: https://github.com/thundersdata-frontend/rn-template

Package versions

  • React: v17.0.1
  • React Native: v0.63.4
  • React Native Reanimated: 1.13.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jakub-gonetcommented, Jan 6, 2021

This is a problem with your jest config, I managed to get your repository to work with that patch:

diff --git a/jest.config.js b/jest.config.js
index b51697d..70c5f50 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -1,18 +1,8 @@
 // eslint-disable-next-line @typescript-eslint/no-var-requires
 const { defaults: tsjPreset } = require('ts-jest/presets');

-const jestModules = ['react-native', '@td-design/react-native'].join('|');
-
 module.exports = {
-  ...tsjPreset,
   preset: 'react-native',
-  setupFiles: ['<rootDir>/jest/setup.js'],
-  setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
-  transform: {
-    ...tsjPreset.transform,
-    '\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
-  },
-  transformIgnorePatterns: [`node_modules/(?!(${jestModules})/)`],
   moduleNameMapper: {
     'test-utils': '<rootDir>/jest/test-utils.js',
     '@td-design/react-native': '<rootDir>/node_modules/@td-design/react-native/src/index.ts',
@@ -22,5 +12,6 @@ module.exports = {
       babelConfig: true,
     },
   },
-  cacheDirectory: '.jest/cache',
+  transformIgnorePatterns: [],
+  setupFiles: ['./jest/setup.js'],
 };
diff --git a/src/screens/homepage/index.tsx b/src/screens/homepage/index.tsx
index 4ac43f4..7b0152b 100644
--- a/src/screens/homepage/index.tsx
+++ b/src/screens/homepage/index.tsx
@@ -10,7 +10,6 @@ export default function Homepage() {
       <Box>
         <Text>hello, td-design</Text>
         <WhiteSpace />
-        <Text>hello, react-native</Text>
       </Box>
     </Container>
   );

This is related to Jest not transforming node_modules/.

0reactions
chj-damoncommented, Jan 6, 2021

@jakub-gonet Thank you so much! You saved my day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing with Jest | React Native Reanimated - Software Mansion
Reanimated test mocks use web implementation of Reanimated2. Before you begin using Reanimated mocks you need some setup actions.
Read more >
"TypeError" when trying to render react-native-reanimated ...
What is particularly strange here is that I am not directly using react-native-reanimated in my code base, and the only library components I...
Read more >
Testing with Jest - React Navigation
Testing with Jest. Testing code using React Navigation takes some setup since we need to mock some native dependencies used in the navigators....
Read more >
TypeError" when trying to render react-native-reanimated ...
Coding example for the question "TypeError" when trying to render react-native-reanimated node in Jest test-React Native.
Read more >
failed to initialize react-native-reanimated library - You.com
config.js (you should have 'react-native-reanimated/plugin' listed there - also see the aboveisted there ...
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