How do you test a component using a native module?
handling native modules in Jest.
native code is absent in the Node-based Jest environment, so you mock the module with jest.mock to return fake values, often via the library's provided jest mock.
WHAT THIS TESTS: Awareness that Jest has no native layer and the disciplined way to mock native modules at the right boundary.
A GOOD ANSWER COVERS: Jest executes in a Node environment without the iOS or Android native runtime, so any native module is unavailable; calling react-native-device-info methods would fail or return undefined because the underlying native implementation is not linked. The solution is to mock the module. Many libraries ship a ready-made Jest mock you register in your Jest setup; otherwise you use jest.mock to replace the module with an object whose methods return deterministic fake values, or add a manual mock under a __mocks__ folder. With the module stubbed, you render the component and assert it behaves correctly for given inputs, such as showing the right text for a mocked device id or OS version. You also test edge values by changing what the mock returns.
COMMON WRONG ANSWERS: Expecting real device values in Jest. Mocking broad parts of React Native rather than the specific module, which hides real bugs. Forgetting to register the mock in the Jest setup file so it applies globally. Mocking at the component level rather than the module boundary.
LIKELY FOLLOW-UPS: Where to put global mocks in jest.config setupFiles, how the new architecture changes native module mocking, the difference between automatic and manual mocks, and when an integration or E2E test is warranted instead.
ONE CONCRETE EXAMPLE: A component renders the app version from getVersion. The test calls jest.mock on react-native-device-info so getVersion returns a fixed string, renders the component, and asserts that version text appears. Changing the mock's return lets a second test verify formatting for a different version without any native code.
Read the original → reactnative.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.