Mocking Native Module with React Native Jest

237 Views Asked by At

I want to mock a native module for jest test case.

I tried this -

jest.mock('NativeModules', () => {
  return {
     UtilityFunc.getConstants: jest.fn(),
  };
});

I know we can't access a property in such a way. My Native module is UtilityFunc and my property is getConstant that returns appversion.

react-native-device-info - I'm using this library as native module for internal stable usage.

Pls share how to use mock with nativeModules in react native.

1

There are 1 best solutions below

0
On

In my project, I have createCalendarEvent native event, and in that, I am getting promise, this is how you can mock your event.

jest.mock('react-native', () => {
 const RN = jest.requireActual('react-native');
 RN.NativeModules.CalendarEventsModule = {
createCalendarEvent: jest
  .fn()
  .mockImplementationOnce(() => Promise.resolve(() => {}))
  .mockImplementationOnce(() => Promise.reject()),
  };
 return RN;
});