# Unit Testing App Logic

> Test pure logic, hooks, view models and data layers in isolation with Jest, fluttertest, JUnit or XCTest, using fakes for network and storage. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/mobile/unit-testing
Last updated: 2026-11-28

Test pure logic, hooks, view models and data layers in isolation with Jest, flutter_test, JUnit or XCTest, using fakes for network and storage.

Intermediate · about 6 hours · difficulty 2/5

Learn first: [App-wide State Tools](https://www.internhack.in/roadmaps/tracks/mobile/state-libraries), [Networking Clients & Server-state Caching](https://www.internhack.in/roadmaps/tracks/mobile/networking-clients)

Unit tests are the cheapest safety net you have: they run in milliseconds on your laptop and CI, with no device or emulator. They pay off most when your logic lives outside UI code.

### What belongs in unit tests

- Formatting, validation and calculation functions.
- Reducers, stores, custom hooks, view models and BLoCs.
- Repositories and mappers that turn API JSON into app models.
- Edge cases: empty lists, missing fields, time zones, currency rounding.

### Tools by stack

- **React Native:** **Jest** (preconfigured by the `jest-expo` preset) with `describe`, `it` and `expect`. Test hooks with `renderHook` from React Native Testing Library.
- **Flutter:** the `test` and `flutter_test` packages; `mocktail` for mocks.
- **Android:** **JUnit** with `kotlinx-coroutines-test` (`runTest`) and Turbine for flows; MockK for mocks.
- **iOS:** **XCTest** or the newer **Swift Testing** (`@Test`, `#expect`).

### Designing for testability

- Inject dependencies (API client, clock, storage) rather than importing singletons, so tests can pass fakes.
- Prefer **fakes** (a simple in-memory implementation) over deep mocks; they break less when internals change.
- Keep tests deterministic: control time, random numbers and async scheduling.

### Arrange, act, assert

```
it("applies a 10% discount over 100", () => {
  expect(applyDiscount(120)).toBe(108);
});
```

Name tests after behaviour, one reason to fail per test.

**Good enough to move on:** the core logic of your app (validation, state transitions, data mapping) has fast unit tests that run on every commit.

## Key concepts

- **Testing pyramid for mobile**
- **Jest with jest-expo**: React Native
- **flutter_test, JUnit or XCTest**: per stack
- **Testing hooks, stores and view models**
- **Fakes vs mocks**
- **Dependency injection for tests**
- **Testing async code and time**
- **Code coverage as a guide**

## Mini project

Write a unit test suite for a shopping cart store covering add, remove, quantity limits, discounts and persistence to a fake storage layer.

## Check yourself

Your view model calls a real API client imported at the top of the file. How would you restructure it so a test can run without the network?

## Free resources

- [React Native: Testing](https://reactnative.dev/docs/testing-overview) (React Native)
- [Jest: Getting started](https://jestjs.io/docs/getting-started) (Jest)
- [Flutter: Testing](https://docs.flutter.dev/testing/overview) (Flutter)

Previous: [Startup Time & App Size](https://www.internhack.in/roadmaps/tracks/mobile/startup-and-app-size) · Next: [Component & UI Testing](https://www.internhack.in/roadmaps/tracks/mobile/component-testing)

## More in Testing Mobile Apps

- [Unit Testing App Logic](https://www.internhack.in/roadmaps/tracks/mobile/unit-testing)
- [Component & UI Testing](https://www.internhack.in/roadmaps/tracks/mobile/component-testing)
- [End-to-End Testing with Maestro & Detox](https://www.internhack.in/roadmaps/tracks/mobile/e2e-testing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Mobile App Developer Roadmap](https://www.internhack.in/roadmaps/tracks/mobile)
