Jest-expo expo-constants undefined

  1. SDK Version: 47
  2. Platforms(Android/iOS/web/all): Android/IOS

Hello everyone,

I have an issue with jest-expo and expo-constants. It seems that my expo-constants object is undefined in my tests. I am using some extra variable in my app.config.tsand their are all undefined, that make my tests fail.

The problem with the constants only occure with tests and is working perfectly when launching the app.

My jest config in package.json:

  "jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
    ]
  },

Is this a known problem ? My problem looks like this issue Jest Constants.manifest.extra undefined but I couln’t find an answer on it.

Hello,

I found a solution with jest.mock() as follow:

jest.mock('expo-constants', () => ({
    expoConfig: {
        extra: {
            BUILD_MODE: process.env.BUILD_MODE,
        },
    },
}));

The only constraint was to load my .env with the dotenv package first, I managed to do that with a setupFiles:

jest.config.ts

export default {
    setupFiles: ['./jest.setup.ts'],
    preset: 'jest-expo',
    transformIgnorePatterns: [
        'node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)',
    ],
};

jest.setup.ts

import dotenv from 'dotenv';

dotenv.config();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.