WhoBuilt

Going 100% TDD with React Native Testing Library

Oct 19, 2021· 26 min· 5.4K views·Watch on YouTube

Speaker

Louis Zawadzki, a developer at Paris-based mobile agency BAM, explains how to apply test-driven development to React Native at React Native EU 2021, hosted by Callstack.

Why use TDD

Zawadzki contrasts slow cycles of rebuilding an app and checking it on a device with the faster feedback of running tests. He outlines TDD’s red, green, and refactoring phases: write a failing test, make it pass quickly, then clean up the code.

Test a change to fetched stories

Using Approval Keynote, a Hacker News clone, Zawadzki demonstrates switching from top stories to new stories. He adds mocked API responses and a test that waits for the new titles, then updates the effect to fetch the selected story type. Checking the app reveals an unhandled missing URL, which he fixes and identifies as a case he should have tested.

Use snapshots to guide visual changes

For a read-state design change, Zawadzki says he uses snapshots sparingly, typically one per page, because they can be hard to understand and maintain. Before coding, he checks which snapshots exist and which should change, then reviews and updates the expected snapshots after adjusting the title style.

Test opening links and build confidence

Zawadzki adds a test that expects React Native’s Linking API to open an article URL, then implements the call with a check for an undefined URL. He recommends learning refactoring, Jest, and React Native Testing Library first, and starting TDD with features developers already know how to test.

TDD improves test practice and architecture

Zawadzki says applying TDD helped him learn his testing tools, treat test files as maintainable code, and cover more features. He also found that making code easier to test often made its architecture easier to understand and change.

Takeaways

  • Use short red, green, and refactoring cycles to get faster feedback than repeated device checks.
  • Start TDD with features you already know how to test, then learn unfamiliar cases as they arise.
  • Mock API responses and test the user-visible result when adding data-fetching behavior.
  • Treat snapshot updates as deliberate: know which pages should change and inspect unexpected differences.
  • Making code easier to test can also make it easier to understand and maintain.

Mentioned in this talk

Search inside this talk

What was claimed 12

Related talks