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.

Level: IntermediateWatch: 14 steps · ~1 minutePlant a bug, run a layerRecall: 3 questions · 8 cardsCompare 4 suitesJest · RNTL · Maestro

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.

4Compare them all
Unit · Jestunit 60 · component 20 · E2E 4
Simulator · not used: Jest runs in Node
API
Unit · 60 × ~5 ms
fixed data · mocks
jest cart.test.ts → …
Step 1 / 14 · step by step
–this run (rough)
0 / 0passed / failed
–the planted bug

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.

Step 4 · Every option at once, on one clock

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.

Bug 1 / 3: The total is wrong (quantity forgotten)
Unit tests only300 ms a runcaught 0 of 0

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

LayerToolRuns onGood at
unitJestNodelogic, edge cases, fast
componentReact Native Testing LibraryNode, native mockeda screen as people use it
E2EMaestro, Detoxa simulator or a phonethe 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

cart.test.ts
// 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");
});
Run: 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 them

The 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.

Table: the three layers

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.