Jest tests fails after Expo and Sentry-Expo upgrade

#1

Hello all,

I just updated my Expo SDK to 20.0 and Sentry-Expo from 1.3 to 1.6, and now many of my test suites fail with the following error:

Test suite failed to run

/Users/[myusername]/Documents/git/[appname]/node_modules/sentry-expo/index.js:3
import { Constants } from 'expo';
^^^^^^
SyntaxError: Unexpected token import

I tried downgrading to sentry-expo 1.3 (cleaning out node_modules and reinstalling all dependencies), but with no luck.

I also tried adding the jest-expo package and preset, but it didn’t help. Until now i’ve been using plain jest and the react-native preset with no issues.

Additionally, I tried adding the babel-preset-expo to the test flavor of .babelrc as per this post, but it didn’t help either.

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    },
    "test": {
      "presets": ["babel-preset-expo"]
    }
  }
}

I’m out of ideas, what could cause this problem all of a sudden?

Thanks :slight_smile:

1 Like
#2

So I found the cause almost immediately after writing the question.

I had added imports to expo-sentry in some files that the failing suites touched on for some crash testing with Sentry, and hadn’t removed the imports after removing the crashes. This caused the error, however I don’t think it should, so the question remains relevant I think?

A quick fix for anyone landing here on a search for a solution is to remove Sentry from the files included in the failing suites.

1 Like
#3

The problem still stands. If you want to test any module that has references to sentry-expo, you get this error. This effectively means I can’t test important modules like my networkClient which uses Sentry to report request or network errors.

I’ve tried mocking all references to sentry using

jest.mock('./../../utils/sentryUtils');
jest.mock('sentry-expo');
jest.mock('expo');

but the suites still fail to run. I’ve also tried adding

"transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation|sentry-expo|expo)"
    ],

to my Jest setup. This just leads to the next error

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

I must be doing something obvious wrong, because I can’t be the only one with this requirement, and there seem to be no mentions anywhere else of this problem. @ccheever - did you find another solution?

#4

So, once gain, I found the solution shortly after posting.

In my case, I needed to mock the sentryUtils file in ./utils/__mocks__/sentryUtils.ts with the following

export const configure = jest.fn();
export const captureException = jest.fn();

which are the two methods I currently use in the module. Then, in the test suites that are affected by the module, I declare it mocked in the beginning of my file:

jest.mock('./../../../../utils/sentryUtils.ts');

Notice the file extension - it’s necessary in my case, probably because of the way Jest and TS is set up. It’s not needed for regular import statements, but apparently it is for jest mocks.

I hope this helps someone in the future, perhaps even myself when I forget about it again :wink:

2 Likes