Automated Testing

Article · 3:00

Jest for logic, React Native Testing Library for components, Maestro for the handful of journeys that must never break.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Three levels, each answering a different question, and each costing roughly ten times the one below it to run.

Jest tests plain functions: a validator, a reducer, a date formatter, the client that builds a request. These are fast enough to run on every save and should cover anything with branching logic.

React Native Testing Library renders a component and interacts with it the way a person would. Its guiding rule is to query by what the user perceives - the text on screen, the accessibility label - rather than by a test id. That has a happy side effect: a component that is hard to query is usually a component that is hard for a screen reader too, so writing these tests improves your accessibility.

Maestro drives the real app on a real device or emulator. Its tests are YAML, it waits for elements automatically rather than needing sleeps, and it is dramatically less brittle than the end-to-end tools that came before. Keep the suite small - sign-in, the main journey, checkout - because these are slow and every one you add is one you maintain.

The proportion that works: many unit tests, a reasonable number of component tests, a handful of end-to-end tests. Inverting it gives you a suite that takes twenty minutes and fails for reasons unrelated to your change.

TS Example 1 46 lines
// 1. Jest - plain logic, milliseconds, run on every save.
import { signUpSchema } from '../schema';

describe('signUpSchema', () => {
  it('rejects mismatched passwords', () => {
    const result = signUpSchema.safeParse({
      email: 'a@b.com', password: 'longenough', confirm: 'different',
    });
    expect(result.success).toBe(false);
  });
});

// 2. React Native Testing Library - query by what a person perceives.
import { render, screen, fireEvent, waitFor } from '@testing-library/react-native';
import { SignUpForm } from '../SignUpForm';

it('shows a message when the email is not an email', async () => {
  render(<SignUpForm />);

  // By placeholder and role, not by test id - the same things a screen
  // reader uses, which is why these tests improve accessibility.
  fireEvent.changeText(screen.getByPlaceholderText('Email'), 'not-an-email');
  fireEvent.press(screen.getByRole('button', { name: 'Create account' }));

  await waitFor(() => {
    expect(screen.getByText(/does not look like an email/i)).toBeVisible();
  });
});

// 3. Maestro - .maestro/sign-in.yaml, driving the real app.
//
// appId: com.example.myapp
// ---
// - launchApp:
//     clearState: true
// - tapOn: "Sign in"
// - tapOn:
//     id: "email"
// - inputText: "test@example.com"
// - tapOn:
//     id: "password"
// - inputText: "correct horse battery staple"
// - tapOn: "Continue"
// - assertVisible: "Today"        # waits automatically - no sleep needed
//
// Run it:  maestro test .maestro/sign-in.yaml

Open this example in the editor and change it. Nothing you type is saved.