What if you could test your app's camera without ever touching a real camera?
Why Mocking native modules in React Native? - Purpose & Use Cases
Imagine you are building a React Native app that uses the device camera or GPS. You want to test your app's features, but the real device hardware is not always available or reliable during testing.
Testing with real native modules manually is slow and tricky. You must have the actual device ready, and tests can fail because of hardware issues or environment differences. This makes debugging frustrating and wastes time.
Mocking native modules lets you replace real device features with simple fake versions during testing. This way, you can run tests quickly and reliably without needing the actual hardware.
import Camera from 'react-native-camera'; // Tests fail if camera not available
jest.mock('react-native-camera', () => ({ Camera: { takePictureAsync: jest.fn(() => Promise.resolve({ uri: 'fake.jpg' })) } }));
Mocking native modules enables fast, reliable testing of app features that depend on device hardware, even when the hardware is not accessible.
When building a photo app, you can mock the camera module to simulate taking pictures in tests, so you can focus on app logic without needing a real camera every time.
Manual testing with real native modules is slow and unreliable.
Mocking replaces real modules with simple fakes for testing.
This makes tests faster, stable, and easier to run anywhere.
Practice
Solution
Step 1: Understand mocking concept
Mocking replaces real native code with fake versions to isolate tests from native dependencies.Step 2: Identify purpose in testing
This helps test JavaScript logic without needing actual native modules running.Final Answer:
To replace native code with fake implementations for easier testing -> Option AQuick Check:
Mocking = Replace native code for testing [OK]
- Thinking mocking speeds up production
- Confusing mocking with adding features
- Believing mocking converts JS to native
NativeModuleExample using Jest?Solution
Step 1: Recall Jest mock syntax
Jest uses jest.mock(moduleName, factoryFunction) to mock modules.Step 2: Check correct usage
jest.mock('NativeModuleExample', () => ({ method: jest.fn() })); correctly uses jest.mock with a factory returning an object with mocked methods.Final Answer:
jest.mock('NativeModuleExample', () => ({ method: jest.fn() })); -> Option DQuick Check:
jest.mock with factory function = jest.mock('NativeModuleExample', () => ({ method: jest.fn() })); [OK]
- Omitting jest. before mock
- Passing object directly instead of factory function
- Using wrong parentheses or braces
jest.mock('NativeModule', () => ({ getValue: () => Promise.resolve('mocked') }));
NativeModule.getValue().then(result => console.log(result));
What will be logged to the console?Solution
Step 1: Understand the mock function
The mocked getValue returns a resolved Promise with string 'mocked'.Step 2: Analyze the then callback
The then callback logs the resolved value, which is 'mocked'.Final Answer:
mocked -> Option CQuick Check:
Promise resolves to 'mocked' logged [OK]
- Expecting the Promise object to be logged
- Forgetting to return a Promise in mock
- Confusing undefined with resolved value
jest.mock('NativeModule', () => ({ getData: jest.fn }));
But NativeModule.getData.mockReturnValue('test') throws an error. What is the problem?Solution
Step 1: Check jest.fn usage
jest.fn is a function that must be called as jest.fn() to create a mock function.Step 2: Identify error cause
Without parentheses, getData is assigned the function jest.fn itself, not a mock function instance, causing error when using mock methods like mockReturnValue.Final Answer:
jest.fn is missing parentheses, so getData is not a mock function instance -> Option BQuick Check:
Use jest.fn() to create mock function [OK]
- Assigning jest.fn without ()
- Assuming jest.mock auto-mocks functions
- Ignoring import errors
fetchData that normally returns a Promise resolving to an object. Which mock implementation correctly simulates this behavior?Solution
Step 1: Understand original method behavior
fetchData returns a Promise resolving to an object, so mock must do the same.Step 2: Evaluate options
jest.mock('NativeModule', () => ({ fetchData: () => Promise.resolve({ id: 1, name: 'Test' }) })); returns Promise.resolve with correct object, matching original behavior.Final Answer:
jest.mock('NativeModule', () => ({ fetchData: () => Promise.resolve({ id: 1, name: 'Test' }) })); -> Option AQuick Check:
Mock Promise resolve with object = jest.mock('NativeModule', () => ({ fetchData: () => Promise.resolve({ id: 1, name: 'Test' }) })); [OK]
- Returning object directly instead of Promise
- Mocking with rejected Promise when success expected
- Returning string instead of object
Ready to prove it?
Pass the test and this pattern moves unknown
