Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest mock and RN 0.76.1: TypeError: NativeModule.getConstants is not a function #550

Open
andreialecu opened this issue Nov 8, 2024 · 2 comments

Comments

@andreialecu
Copy link
Contributor

After upgrading to RN 0.76.1, the jest mock seems to fail to import:


    TypeError: NativeModule.getConstants is not a function


    >  9 | import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';
         | ^


      at Object.getConstants (node_modules/react-native/src/private/specs/modules/NativeDeviceInfo.js:52:32)
      at Object.getConstants (node_modules/react-native/Libraries/Utilities/Dimensions.js:121:33)
      at Object.require (node_modules/react-native/Libraries/Utilities/PixelRatio.js:13:20)
      at Object.require (node_modules/react-native/Libraries/StyleSheet/StyleSheet.js:29:20)
      at Object.require [as StyleSheet] (node_modules/react-native/index.js:306:12)
      at Object.StyleSheet (node_modules/react-native-safe-area-context/lib/commonjs/SafeAreaContext.js:77:29)
      at Object.require (node_modules/react-native-safe-area-context/lib/commonjs/index.js:7:24)
      at Object.requireActual (node_modules/react-native-safe-area-context/jest/mock.tsx:25:32)
      at Object.require (jest.setup.js:9:1)
@paulschreiber
Copy link

paulschreiber commented Nov 14, 2024

We're seeing the same thing:
https://github.com/techmatters/terraso-mobile-client/actions/runs/11842140912/job/33000063351

In our case, I don't see react-native-safe-area-context in the stack trace. Filed against react-native-paper: callstack/react-native-paper#4554

@Balthazar33
Copy link

I got this for react-native-mmkv-storage.
Sharing the mmkv mock here, in case anyone needs it:

jest.mock('react-native-mmkv-storage', () => {
  return {
    MMKVLoader: jest.fn().mockImplementation(() => {
      return {
        withInstanceID: jest.fn().mockReturnValue({
          initialize: jest.fn().mockResolvedValue({
            setString: jest.fn(),
            getString: jest.fn(),
            removeItem: jest.fn(),
            clearStore: jest.fn(),
            getAllKeys: jest.fn(),
          }),
        }),
        initialize: jest.fn().mockResolvedValue({
          setString: jest.fn(),
          getString: jest.fn(),
          removeItem: jest.fn(),
          clearStore: jest.fn(),
          getAllKeys: jest.fn(),
        }),
      };
    }),
  };
});

Also, I did not add the library's setUp file path in setupFiles in my jest.config file, but appended the package name to transformIgnorePatterns.

I added the mock I've shared above in my jestSetup.js file which has the mocks for the whole app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants