tezvyn:

Unit testing vs component testing in React Native

AI-drafted, machine-checkedSource: interviewbeginner
WHAT IT TESTS

testing taxonomy.

OUTLINE

unit tests verify isolated functions/logic with Jest; component tests render components and assert behavior with React Native Testing Library.

WHAT THIS TESTS: Understanding of the different test levels in a React Native project and the standard tooling for each.

A GOOD ANSWER COVERS: Unit tests verify the smallest isolated units of logic, such as a formatting helper, a reducer, or a custom hook's pure computation, with all external dependencies mocked. They are fast and numerous and run on Jest, the default test runner for React Native. Component tests operate one level up: they render an actual component using a test renderer and assert on the rendered output and its response to user interaction, for example that pressing a button shows a result. The standard tool is React Native Testing Library, which renders into a JavaScript environment and queries elements the way a user would, layered on Jest. Crucially, neither runs on a real device; that is the domain of end-to-end testing.

COMMON WRONG ANSWERS: Treating unit and component tests as interchangeable. Believing component tests need a device or emulator. Saying React Native Testing Library replaces Jest rather than building on it. Confusing component testing with full E2E flows like Detox.

LIKELY FOLLOW-UPS: Where integration tests fit, what snapshot testing is and its tradeoffs, how to test hooks, and when to escalate to Detox.

ONE CONCRETE EXAMPLE: A price-formatting function is covered by a Jest unit test asserting that an input returns a specific string. The PriceTag component that uses it gets a component test with React Native Testing Library: render PriceTag with a value, then assert the formatted text appears on screen and updates when a prop changes, all in Jest without launching a device.

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.