Vyas Reddy
Vyas Reddy

Reputation: 1272

Jest is failing because of missing property in Expo (React Native)

Test suite failed to run:

TypeError: Cannot read property 'linkingUri' of undefined at Object. (node_modules/expo/src/Constants.js:18:29) at Object. (node_modules/expo/src/Logs.js:94:228) at Object. (node_modules/expo/src/Expo.js:3:1)

Any one have the solution for this?

Upvotes: 0

Views: 1840

Answers (1)

ide
ide

Reputation: 20788

The Expo SDK (as well as React Native packages with native code) read fields from ReactNative.NativeModules. In this case, Constants.js in the Expo SDK is trying to read from ReactNative.NativeModules.ExponentConstants.linkingUri, but since one of the objects in that chain is not defined -- after all, you're running this code in Node and not in Expo -- you're getting that TypeError.


jest-expo

There is a package called jest-expo, which mocks out ReactNative.NativeModules.ExponentConstants and much more for Jest tests.

To use jest-expo (as of version 22.x), first remove jest or jest-cli from your package.json file's dependencies since jest-expo will import the correct version of Jest for you. For example:

yarn remove jest

Next, install jest-expo in your project:

yarn add --dev jest-expo

Note: If you're using an older version of the Expo SDK, make sure to use the corresponding version of jest-expo. You can't use jest-expo for Expo SDK 21 with a project built for SDK 22, for example.


Finally, add this to your package.json:

"scripts": {
  "test": "node_modules/.bin/jest"
},
"jest": {
  "preset": "jest-expo"
}

Run your tests with yarn test.


These specific instructions might change over time but they communicate the gist of how to use jest-expo. The GitHub README for the project is one of the best places to look as it's closer to the source of truth.

Upvotes: 2

Related Questions