Are you satisfied with your test suites? If you said no, you’re not alone—most developers aren’t. And testing in React Native is harder than on most platforms. How can you write JavaScript tests when the JS and native code are so intertwined? And what in the world are you supposed to do about that persistent act() warning? Faced with these challenges, some teams are never able to make any progress testing their React Native app, and others end up with tests that don’t seem to help and only take extra time to maintain.
But it doesn’t have to be this way. React Native Testing Library (RNTL) is a great library for component testing, and with the right mental model you can use it to implement tests that are low-cost and high-value. In this three-hour workshop you’ll learn the tools, techniques, and principles you need to implement tests that will help you ship your React Native app with confidence. You’ll walk away with a clear vision for the goal of your component tests and with techniques that will help you address any obstacle that gets in the way of that goal.
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Intro to React Native Testing Library
1. Intro to React Native Testing Library
While we're waiting to start, if you're comfortable, share
your answer to the following question in the chat:
What do you want to learn about testing in this workshop?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
2. Intro to React Native Testing Library
https://rnte.st/london22
Clone the repo, build, and test!
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
5. Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
6. What We'll Cover
— React Native Testing Library
— Jest Native
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
7. What We Won't Cover
— Jest basics
— End-to-end testing (Detox, Appium)
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
8. Learning More Afterward
— https://rnte.st/london22
— Discord community
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
9. 02 Component Tests:
Rendering
Intro to React Native Testing
Library
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
10. What is a component test?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
12. What to test?
— Call methods/functions?
— Check state values?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
13. "We don’t encourage reading implementation details
like state variables themselves…Instead, test observable
behavior — i.e. what your component renders or does."
-- @dan_abramov, 2019-03-06 tweet
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
14. Testing the Contract
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
15. "A component contract is the agreement between a
component and the rest of the application."
-- Edd Yerburgh, Testing Vue.js Applications
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
16. "Other components can assume the component will
fulfill its contractual agreement and produce the agreed
output if it’s provided the correct input."
-- Edd Yerburgh, Testing Vue.js Applications
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
17. Question: what are some of the kinds of inputs and
outputs that components have?
Answer in the chat!
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
18. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
19. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
20. To the code!
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
27. Which Query Should I Use?
https://callstack.github.io/react-native-testing-library/docs/how-should-i-query
"your test should resemble how users interact with your
code (component, page, etc.) as much as possible"
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
28. Which Query Should I Use?
https://callstack.github.io/react-native-testing-library/docs/how-should-i-query
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
29. Note on getByRole()
screen.getByRole('button', {name: 'Save Changes'})
— Likely going to be the top recommendation soon
— Feature needed first: implicit roles (Pressables
queryable as button by default)
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
34. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
35. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
36. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
37. To the code!
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
38. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
39. Testing the Contract
Inputs:
✅
Props
- User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
40. Exercise 1
— intro-to-rntl-exercises
— exercises/1-Exercise1.md
For help:
reactnativetesting.io/component/testing/
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
41. A few more notes about testing
rendering and props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
62. No, mocks are an essential part of
Testing the contract.
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
63. Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
64. Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
✅
Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
65. Exercise 2
— intro-to-rntl-exercises
— exercises/2-Exercise2.md
For help:
reactnativetesting.io/component/testing/#interaction
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
66. Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
67. 04 Component Tests:
Effects and Module Mocks
Intro to React Native Testing
Library
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
68. How can we test effects?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
69. How can we test effects?
Test the effect/result.
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
70. Data loading effects hit the API
Problem?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
71. Options for Mocking Web Service Requests
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
72. Options for Mocking Web Service Requests
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
79. Exercise 3
— intro-to-rntl-exercises
— exercises/3-Exercise3.md
For help:
reactnativetesting.io/component/testing.html
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
80. act() Error
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
81. To the code!
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
82. 05 Wrap-up
Intro to React Native Testing
Library
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
83. What did we learn?
— What does it mean to test the contract?
— Why test the contract?
— What are the inputs and outputs of a component?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
84. Remaining Questions?
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
85. Continuing the Learning
— https://rnte.st/london22
— Discord community
Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22
86. Introduction to React Native Testing Library - Josh Justice - React Advanced London - 2022-10-28 - https://rnte.st/london22