2
votes

I'm developing a mobile app using React-Native and Expo.

Everything is doing great and the app is running as expected.

But my tests are not running when importing some @expo libraries.

The error is the classical one: SyntaxError: Unexpected token import

Giving the full context of the erro:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import glyphMap
                                                                                         ^^^^^^
SyntaxError: Unexpected token import

Googling a solution I ended up most of the time with something similar to what is mentioned here.

So I wrote on my package.json

  "jest": {
    "transformIgnorePatterns": [ 
      "/!node_modules\\/@expo"
    ],
    "preset": "react-native"
  }

I also have tried changing the module name to:

  1. /!node_modules\/expo
  2. /!node_modules\/expo/vector-icons
  3. /node_modules/@expo/vector-icons

I also followed another answer and added .babelrc with the content:

{
  "presets": ["babel-preset-expo", "react-native"],
  "env": {
    "test": {
      "plugins": [
        "babel-plugin-dynamic-import-node"
      ]
    }
  }
}

Unfortunately, none of these solutions worked for me.

To give more context, it is a React Native application with Expo, running on OSX I receive another 5 warnings besides the error above. The warnings are regarding duplicated module names(probably unrelated but anyway):

jest-haste-map: duplicate manual mock found:
  Module name: ErrorUtils
  Duplicate Mock path: /<project_dir>/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in:
/<project_dir>/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js
 Please delete one of the following two files:
 /<project_dir>/node_modules/react-native/node_modules/fbjs/lib/__mocks__/ErrorUtils.js
/project_dir/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js


jest-haste-map: @providesModule naming collision:
  Duplicate module name: camelcase
  Paths: /<project_dir>/node_modules/react-native/node_modules/yargs-parser/node_modules/camelcase/package.json collides with /<project_dir>/node_modules/react-native/node_modules/yargs/node_modules/camelcase/package.json

This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
  Duplicate module name: bser
  Paths: /<project_dir>/node_modules/react-native/node_modules/bser/package.json collides with /<project_dir>/node_modules/react-native/node_modules/fb-watchman/node_modules/bser/package.json

This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
  Duplicate module name: core-js
  Paths: /<project_dir>/node_modules/react-native/node_modules/core-js/package.json collides with /<project_dir>/node_modules/react-native/node_modules/fbjs/node_modules/core-js/package.json

This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
  Duplicate module name: yargs
  Paths: /<project_dir>/node_modules/react-native/node_modules/yargs/package.json collides with /<project_dir>/node_modules/react-native/node_modules/uglify-js/node_modules/yargs/package.json

This warning is caused by a @providesModule declaration with the same name across two different files.

Versions: react-native: 0.46.1 @expo/vector-icons: 5.1.0 jest: 20.0.4

Any other idea?

**** UPDATE ****

Changing the transformIgnorePatterns to: "/!node_modules\/\@expo" doesn't make it work, but at least change to another error.

Invariant Violation: Native module cannot be null.

  at invariant (node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:7)
  at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:1)
  at Object.<anonymous> (node_modules/expo/src/Location.js:6:26)
  at Object.<anonymous> (node_modules/expo/src/Expo.js:8:1)
1
did you figure this outMateen-Hussain

1 Answers

2
votes

I had the same issue. I eventually resolved it by removing and reinstalling jest-expo. Make sure that your version of jest-expo matches your version of Expo.

npm uninstall jest-expo npm cache clean # or npm cache verify (if Node 5+) npm install --save-dev jest-expo