Skip to content
tezvyn:

React Native & Jest: Automatic Native Module Mocking

Source: jestjs.ioMediumHow cards are made

React Native & Jest: Automatic Native Module Mocking

Jest tests for React Native run in Node, not on a device, so native code won't work. The react-native preset automatically mocks native modules, letting you test components without a real device environment.

Why it exists

To test React Native components, you need to run them. But running them on a device or emulator for every small test is incredibly slow. Jest tests run in a fast Node.js environment, which can't execute native Objective-C/Swift or Java/Kotlin code. We need a way to test our JavaScript logic without the native dependencies.

The mental model

Think of the react-native Jest preset as a "stunt double" for the native side of your app. When your component calls for a native feature, like rendering a View, the preset swaps in a lightweight, pure-JavaScript fake that has the same interface. This lets your tests run and verify your component's logic without ever touching actual native code.

How it works

When you configure Jest with preset: 'react-native', it sets up a test environment that mimics a React Native app. It provides default mocks for all core React Native components and modules. For example, when your component renders a <View>, Jest doesn't render a native view. Instead, it uses a mock JavaScript object that represents the View and its props, which is perfect for snapshot testing.

When to use it

This is the standard approach for unit and component testing in React Native. It's ideal for snapshot tests, which verify that your UI doesn't change unexpectedly, and for testing business logic within your components, hooks, and state management. The speed gain is significant because you avoid the overhead of a device or emulator.

When not to use it

Don't rely on these mocks for end-to-end (E2E) or integration tests that need to verify real native functionality. Because the mocks are simple fakes, they can't confirm that a native feature works correctly, that styles render properly on a real device, or that your app integrates correctly with native APIs like the camera or GPS. For that, you need tools like Detox or Appium that drive a real application.

One canonical example

A common issue is testing a component that uses a third-party library with native code. If that library doesn't pre-compile its code, Jest's transformer may ignore it. You must update the transformIgnorePatterns in your jest.config.js to tell Jest to also transform that module's code, like this: transformIgnorePatterns: ["node_modules/(?!(@react-native|some-uncompiled-library)/)"].

Interview question

What is the primary purpose of Jest's 'preset: 'react-native'' in a React Native testing setup?

  • a.To conduct comprehensive end-to-end tests that interact with real device hardware and native APIs.
  • b.To verify the correct compilation and linking of native modules across different platforms.
  • c.To enable fast execution of JavaScript component logic tests by simulating native modules.Correct
  • d.To automatically generate native code stubs for any custom Objective-C/Swift or Java/Kotlin modules.
Why?

The 'react-native' preset allows Jest to run React Native component tests quickly in a Node.js environment by providing JavaScript mocks for native modules. Option A is incorrect because the card explicitly states that these mocks are not suitable for end-to-end tests requiring real native functionality.

Just read this? Test yourself on what you have been reading.

Read the original → jestjs.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.

See open roles