DEV Community

Cover image for Fix problems on running Jest with React native v0.56
DIGITAL SQUAD
DIGITAL SQUAD

Posted on

Fix problems on running Jest with React native v0.56

The combination of Jest and React native version 0.56 may have some problems and these steps should be fix those problems.

yarn test
yarn run v1.7.0
$ jest
 FAIL  __tests__/timeline_form_screen_test.js
  ● Test suite failed to run

  Plugin 0 specified in "/ReactNative/AppName/node_modules/babel-preset-react-native/index.js" provided an invalid property of "default" (While processing preset: "/ReactNative/AppName/node_modules/babel-preset-react-native/index.js")

      at Plugin.init (node_modules/babel-core/lib/transformation/plugin.js:131:13)
      at Function.normalisePlugin (node_modules/babel-core/lib/transformation/file/options/option-manager.js:152:12)
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
          at Array.map (<anonymous>)
      at Function.normalisePlugins (node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:265:14
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:323:22
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)

Enter fullscreen mode Exit fullscreen mode

If you got erro above, rename .babelrc to .babel.config.js on your React Native project root and will fix the error.

However still may have another error.

 /ReactNative/AppName/__tests__/component_test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                                    ^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Enter fullscreen mode Exit fullscreen mode

Modify package.json like below.

"jest": {
    "preset": "react-native"
  }
Enter fullscreen mode Exit fullscreen mode

to

"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }
  }
Enter fullscreen mode Exit fullscreen mode

Then it will fix the error.

Top comments (0)