trick to mocking expo-constants for jest?

Please provide the following:

  1. SDK Version: 46
  2. Platforms(Android/iOS/web/all): jest

I updated jest-expo to v46 and now my Constants are having an issue where they are undefined. I thought trying to mock it would work like so:

  jest.mock('expo-constants', () => {
    return {
      ...jest.requireActual('expo-constants'),
      __esModule: true,
      default: jest.fn(),
      manifest: {
        extra: {
          api: 'ourURL',
        },
      },
    };
  });

I also tried moving this from the jest-setup file into it’s own under the __mocks__ dir, but no difference.

However, Constants is still undefined when running my jest test.

Is there a trick to mocking this package?

I created a separate file that read the constants and the .env file data and provided that to the rest of the system. This then allowed me to easily mock that file and also take an inventory of what data we need from the environment.

the thing is that the Expo app.config file seemingly isn’t imported during a jest run. We have a fallback in this config which isn’t coming through.

That’s correct, which is why I created a separate file to abstract away all these system variables like .env, app.config FileSystem etc.

It makes for easy mocking later too.

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