Testing a React Native app
Fill the pyramid one layer at a time and plant a few bugs on the way: see which tests stay green while the app is broken, and which ones break while the app is fine.
What you'll take away
- Build the test pyramid: many Jest unit tests, fewer component tests, a few E2E flows
- Find a button by its role and visible words, so renaming a testID breaks nothing
- Tell which layer catches a wrong total, a stuck button and a build on the wrong API
- Choose Maestro or Detox, and save the slow E2E flows for the paths that matter
The 1-minute version
Tap a point and the picture below plays that step.
Plant "release broken" and run each layer: see which ones stay green while the app is broken. E2E taps through the app on the phone, one command at a time
The release build points at the wrong API server. Which kind of test catches it?
The picture: the phone on the left is the simulator, dimmed when a layer doesn't use it; a component test's render in memory is drawn on it as a wireframe. To its right, the CI yard's pyramid (each window is one test), the stand of mocks the lower layers use, the API tower and an old test server. Times are rough orders of magnitude from a model, not measurements.
Compare them all: Test suites
The same app with three planted bugs: a wrong total, a stuck Checkout button, and a release build pointing at the wrong API. Each tile is a different suite: how many bugs it catches, and how long a full run takes.
Use it when A library or pure logic with no screens
Skip it when An app with screens: it never sees whether a button works
A model: the time per test is a rough order of magnitude (unit milliseconds, component around a hundred milliseconds, E2E seconds), not measured on a real app. Press any tile to see it large above, with when it fits.
The three layers
| Layer | Tool | Runs on | Good at |
|---|---|---|---|
| unit | Jest | Node | logic, edge cases, fast |
| component | React Native Testing Library | Node, native mocked | a screen as people use it |
| E2E | Maestro, Detox | a simulator or a phone | the real app, the real build |
Find things the way people do
getByRole("button", { name: "Checkout" }) breaks when a person would have trouble; getByTestId("btn-2") breaks when someone renames an id. A labelled button also helps screen readers.
In real code
// Unit tests: plain functions, in Node, in milliseconds. No phone, no rendering.
import { baht, canCheckout, cartTotal } from "./cart.ts";
const lines = [
{ name: "Mug", price: 290, qty: 2 },
{ name: "Cap", price: 250, qty: 1 },
];
test("the total multiplies each price by its quantity", () => {
expect(cartTotal(lines)).toBe(830);
});
test("checkout needs at least one item", () => {
expect(canCheckout([])).toBe(false);
expect(canCheckout(lines)).toBe(true);
});
test("prices read the way the screen shows them", () => {
expect(baht(1290)).toBe("฿1,290");
});
npx jest cart.test.ts (really run with Jest: plain logic, no phone needed)It printed: jest 30.5.2: 3 of 3 passed · the total multiplies each price by its quantity · checkout needs at least one item · prices read the way the screen shows themThe cart's logic called directly: really run with Jest, in milliseconds.
Common questions
What kinds of tests does a React Native app need?
Three layers: unit tests with Jest for logic, component tests with React Native Testing Library for screens, and E2E with Maestro or Detox for the main paths on the real app. Many at the bottom, few on top.
Do component tests run on a phone?
No. React Native Testing Library renders the screen in memory, in Node, with the native parts mocked: fast and easy to run in CI, but those real parts are never tried. That's what E2E is for.
Why not find things by testID?
A testID is a name in the code that users never see: rename it and the test breaks though the app is fine, and it passes even if the button has no readable words. Find by role and visible text, so tests break when a person would really have a problem.
Maestro or Detox?
Maestro writes flows as short YAML and is easy to start with; it sees the app from outside. Detox writes JavaScript, knows React Native's insides and waits for the app to be idle. Both drive the real app on a simulator or a phone.
If I have E2E, do I still need unit tests?
Yes. E2E catches widely but slowly (a full run can take minutes), and when it fails it only says the screen looks wrong. A unit test catches a logic bug in milliseconds and names the function.
All unit and component tests pass. Why is the real app broken?
Because those layers use mocks: a fake API, fake native modules, and the real build's config never tried. A bug like a build pointing at the wrong API shows up only when E2E runs that build.
Next lesson
Ship to the storesLevel: Intermediate
The app is tested; next, getting the build to people: bundle ID, build numbers, signing, TestFlight, review and over-the-air updates.