SlideShare a Scribd company logo
Testing React Native Apps
As you get settled, write the answer to the following
question on Post-It notes. Write as many answers on
separates notes as you like. When you're done, put them
on the back wall.
What do you want to learn about testing in this workshop?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps
https://rnte.st/cr23
Clone the repo, build, and test!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
ReactNativeTesting.io
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Josh Justice
CodingItWrong.com
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Thank you, assistants!
— David Leuiliette
— Jon Major Condon
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Answer the following two questions on Post-It notes.
Then discuss your answers with your group. Choose
someone who will summarize your group's answers to
the whole workshop afterward.
1. What benefits are you currently getting from
testing?
2. What are your testing pain points?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Why Test?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
My Story
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing Pain Points
— Difficult to test
— Flaky tests
— Fragile tests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Example-Based Tests
— Component tests (React Native Testing Library)
— End-to-end tests (Detox)
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Learning More Afterward
— https://rnte.st/cr23
— Discord community
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Questions and thoughts
welcome!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Stretch break!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
02 Component Tests:
Rendering
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
What is a component test?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
React Native Testing Library
@testing-library/react-native
callstack.github.io/react-native-testing-library
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
What to test?
— Call methods/functions?
— Check state values?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
"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
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
"A component contract is the agreement between a
component and the rest of the application."
-- Edd Yerburgh, Testing Vue.js Applications
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
"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
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Discuss:
What are some of the kinds
of inputs and outputs that
components have?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Getter Functions
— screen.getByText
— screen.getByLabelText
— screen.getByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Text
return <Text>Hello, world!</Text>;
screen.getByText('Hello, world!')
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
TextInput
return <TextInput placeholder="Enter your name" ... />;
screen.getByPlaceholderText('Enter your name')
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Image
return (
<Image source={...} accessibilityLabel="squirrel waving" />
);
screen.getByLabelText('squirrel waving')
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
SVG
import WavingHand from './assets/waving-hand.svg';
return <WavingHand accessibilityLabel="waving hand" />;
screen.getByLabelText('waving hand')
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Which Query Should I Use?
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"
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Which Query Should I Use?
callstack.github.io/react-native-testing-library/docs/how-should-i-query
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Note on getByRole()
screen.getByRole('button', {name: 'Save Changes'})
— May become the top recommendation soon
— Feature needed first: implicit roles (Pressables
queryable as button by default)
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Confirming Presence
expect(screen.getBy…()).toBeVisible();
expect(screen.getBy…()).toBeOnTheScreen();
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Confirming Absence
expect(screen.queryBy…()).not.toBeOnTheScreen();
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Getter Functions
— screen.getByText
— screen.getByLabelText
— screen.getByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Query Functions
— screen.queryByText
— screen.queryByLabelText
— screen.queryByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
- User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Exercise 1
— rn-testing-exercises
— exercises/Exercise1.md
For help:
reactnativetesting.io/component/testing/
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
A few more notes about testing
rendering and props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing content,
not appearance.
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
What about snapshot tests?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
import renderer from 'react-test-renderer';
import Hello from './Hello';
it('renders correctly', () => {
const tree = renderer.create(<Hello name="Josh" />).toJSON();
expect(tree).toMatchSnapshot();
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
03 Component Tests:
Actions and Mocks
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
- User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
- User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
import {fireEvent, render, screen} from '@testing-library/react-native';
describe('NewMessageForm', () => {
describe('pressing send', () => {
it('clears the message field', () => {
render(<NewMessageForm />);
fireEvent.changeText(
screen.getByPlaceholderText('Message'),
'Hello world',
);
fireEvent.press(screen.getByText('Send'));
expect(screen.getByPlaceholderText('Message')).toHaveProp('value', '');
});
});
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
fireEvent
import {fireEvent} from '@testing-library/react-native';
fireEvent.press(element)
fireEvent.changeText(element, text)
fireEvent.scroll(element, eventData)
fireEvent(element, eventName, eventData)
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
import {render, fireEvent} from '@testing-library/react-native';
describe('NewMessageForm', () => {
describe('pressing send', () => {
it('clears the message field', () => {
render(<NewMessageForm />);
fireEvent.changeText(
screen.getByPlaceholderText('Message'),
'Hello world',
);
fireEvent.press(screen.getByText('Send'));
expect(screen.getByPlaceholderText('Message')).toHaveProp('value', '');
});
});
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
toHaveProp
Provided by Jest Native
expect(...).toHaveProp('propName', 'propValue');
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
- User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Mock Functions
jestjs.io/docs/mock-functions
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Creating a New Mock Function
const myMock = jest.fn().mockName('myMock');
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Mock Matchers
expect(myMock).toHaveBeenCalled();
expect(myMock).toHaveBeenCalledWith(arg, secondArg, ...);
expect(myMock).toHaveBeenCalledWith(expect.any(String));
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
it('calls the send handler', () => {
const messageText = 'Hello world';
const sendHandler = jest.fn().mockName('sendHandler');
render(<NewMessageForm onSend={sendHandler} />);
fireEvent.changeText(
screen.getByPlaceholderText('Message'),
messageText,
);
fireEvent.press(screen.getByText('Send'));
expect(sendHandler).toHaveBeenCalledWith(messageText);
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Question:
Do you think mocks involve
testing implementation
details or not?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
My view:
Mocks are an important
part of testing the contract.
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
- Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
✅
Props
✅
User interaction events
Outputs:
✅
Rendered UI
✅
Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Exercise 2
— rn-testing-exercises
— exercises/Exercise2.md
For help:
reactnativetesting.io/component/testing/#interaction
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing the Contract
Inputs:
— Props
— User interaction events
Outputs:
— Rendered UI
— Calls to function props
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
04 Component Tests:
Effects and Module Mocks
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
How can we test effects?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
How can we test effects?
Test the result/"effect".
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Data loading effects hit the API
Problem?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Options for Mocking Web Service Requests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Options for Mocking Web Service Requests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Jest Module Mocks
jestjs.io/docs/mock-functions#mocking-modules
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
import {render, screen} from '@testing-library/react-native';
import RestaurantContainer from './RestaurantContainer';
import api from './api';
jest.mock('./api');
describe('RestaurantContainer', () => {
it('loads restaurants upon first render', async () => {
api.get.mockResolvedValue({/* the response object */});
render(<RestaurantContainer />);
expect(await screen.findByText('Pasta Place')).toBeVisible();
expect(screen.getByText('Salad Place')).toBeVisible();
expect(api.get).toHaveBeenCalledWith('/restaurants');
});
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
mockedModule.myfunc.mockReturnValue(val);
mockedModule.myfunc.mockResolvedValue(val);
mockedModule.myfunc.mockRejectedValue(val);
jestjs.io/docs/mock-function-api
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Getter Functions
— screen.getByText
— screen.getByLabelText
— screen.getByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Query Functions
— screen.queryByText
— screen.queryByLabelText
— screen.queryByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Find Functions
— await screen.findByText
— await screen.findByLabelText
— await screen.findByPlaceholderText
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Exercise 3
— rn-testing-exercises
— exercises/Exercise3.md
For help:
reactnativetesting.io/component/testing
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
act() Error
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Component Testing
As a group, come up a way to explain what "testing the
contract" of a component means to other developers.
Use the poster in your area. You can write a bulleted list,
use diagrams, draw a picture to use a metaphor, etc.
Feel free to get creative!
After, your group will present your board to the
workshop.
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
05 End-to-End Testing:
Basics
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
E2E Tests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Choosing an E2E Test
Library
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
— Runs on Hardware
— Runs on Sauce Labs and AWS
Device Farm
— Runs completely outside your
app
— Have to use multiple libraries
together
— Hard to find complete
documentation
— Tests tend to be flaky
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Detox
— Write tests in JavaScript or
TypeScript
— Has inside knowledge of your
app
— Expo not officially supported
— Can be complex to set up
— Does not run on physical iOS
devices yet
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Maestro
— Easy setup, a single library
— Runs completely outside your
app
— Maestro Studio to
interactively write commands
— New; rough edges
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
it('detects text', async () => {
await expect(element(by.label('Hello, Lecture 5!'))).toBeVisible();
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
element(…)
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
it('should have welcome screen', async () => {
await expect(
element(by.label('Welcome to React Native!'))
).toBeVisible();
});
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Matchers
by.id()
by.text()
by.label()
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
await expect(element(by.id('welcome')))
.toBeVisible();
await expect(element(by.id('welcome')))
.toHaveText('Welcome to React Native!');
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Expectations
toExist()
toBeVisible()
toHaveText()
toHaveValue()
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
await element(by.id('mybutton')).tap();
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
await element(by.id('mytextinput'))
.typeText('I typed this');
await element(by.id('mytextinput'))
.clearText();
await element(by.id('mytextinput'))
.typeText('Something else');
await element(by.id('mytextinput'))
.replaceText('Something else');
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Actions
tap()
typeText()
clearText()
replaceText()
scroll()
swipe()
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
What to E2E test?
Main paths, not all edge cases
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
06 End-to-End Testing:
External Services
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Connecting to the real backend:
Upsides?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Connecting to the real backend:
Downsides?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Options for Mocking Web Service Requests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Local web server?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Options for Mocking Web Service Requests
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Fake Module
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
What's a fake?
A version of the service that acts similar to the real thing
but stores its data in-memory
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To the code!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
API file setup
api.js
api.mock.js
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
api.js
import axios from 'axios';
const api = axios.create({
baseURL: 'https://api.reactnativetesting.io',
// other config
});
export default api;
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
api.mock.js
const api = {
get() {
return Promise.resolve({
data: [
{id: 1, name: 'Widget 1'},
{id: 2, name: 'Widget 2'},
]
});
},
// ...
};
export default api;
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Configure metro.config.js
— Prefer .mock.js extension when present
— Exact code may differ if you've modified
metro.config.js for other reasons
— E.g. using react-native-svg-transformer
— See reactnativetesting.io/e2e/external-services
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
package.json
"start": "react-native start",
+"start:mock": "MOCK_API=true npm run start",
"test": "jest"
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
For Windows support
"start": "react-native start",
+"start:mock": "cross-env MOCK_API=true npm run start",
"test": "jest"
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Exercise 4
— rn-testing-exercises
— exercises/Exercise4.md
For help:
— reactnativetesting.io/e2e/testing
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Recommendation:
— Automate testing against a fake
— Manually test integration with real service
— If you need to automate testing with real service, do
a small separate test suite
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
End-to-End Testing
As a group, pick one of your group members' apps and
come up with a list of the most valuable scenarios you
would end-to-end test. Write each on a Post-It note and
put it on your poster. If there are any you're uncertain
how you would write a test for, discuss them as a group.
Nominate someone to present your scenarios and
questions to the whole workshop.
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
07 Wrap-up
Testing React Native Apps
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
How much of each type of
test to do?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Tradeoffs
— End-to-End Tests: give confidence the whole app
works
— Unit and Component Tests: faster, easier, less flaky
testing of edge cases, influence design of code
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
My Recommendations
— End-to-end test main flows only
— Component test edge cases
— Extract plain functions and classes when it makes
sense, and unit test them
— Tweak as you go
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
To Discuss
Write the answer to the following question on Post-It
note. Use as many as you need:
Based on what you learned today, what specifically do
you want to do differently testing your React app
going forward?
Afterward we'll go around the room and each share our
answers!
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Remaining Questions?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Remaining Questions?
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Continuing the Learning
— https://rnte.st/cr23
— https://reactnativetesting.io
— Discord community
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Effective Detox Testing
Oct 25, 2023 -Virtual
— CI on GitHub Actions
— Detox local development
workflow
— When Detox vs RNTL vs
manual
— How much to test with Detox
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
Thanks!
https://rnte.st/cr23survey
Josh Justice
josh.justice@testdouble.com
https://rnte.st/cr23
Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23

More Related Content

Similar to Testing React Native Apps - Chain React 2023 (20)

PPTX
Session 03_04-Working with React Native.pptx
VHiu94
 
PPTX
Intro to React Native
ForSharing
 
PDF
React native
Vishal Dubey
 
PPTX
React native
Jacob Nelson
 
PDF
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Indonesia
 
PPTX
Session 01_02-Introduction to React Native .pptx
VHiu94
 
PPTX
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
PDF
Don't let your tests slow you down
Daniel Irvine
 
PDF
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
PPTX
React Native - Unleash the power of React in your device - Eduard Tomàs - Cod...
Codemotion
 
PDF
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Codemotion
 
PDF
ReactJS Vs React Native with 10 Significant Differences
Windzoon Technologies
 
PPTX
React Native is Ready for Prime Time — Team × Technology (React Conf 2016)
James Ide
 
PPTX
React Native Interview Questions.pptx
Gets Solution
 
PDF
A Closer Look At React Native
Ian Wang
 
PPTX
Introduction to react native @ TIC NUST
Waqqas Jabbar
 
PDF
The Many Ways to Test Your React App
All Things Open
 
PDF
Which Tools Do React Native Developers Use to Create Interactive User Interfa...
vitaragaistechnolabs
 
PDF
[QE 2018] Adam Stasiak – Nadchodzi React Native – czyli o testowaniu mobilnyc...
Future Processing
 
PPTX
Introduction to React Native
Waqqas Jabbar
 
Session 03_04-Working with React Native.pptx
VHiu94
 
Intro to React Native
ForSharing
 
React native
Vishal Dubey
 
React native
Jacob Nelson
 
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Indonesia
 
Session 01_02-Introduction to React Native .pptx
VHiu94
 
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
Don't let your tests slow you down
Daniel Irvine
 
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
React Native - Unleash the power of React in your device - Eduard Tomàs - Cod...
Codemotion
 
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Codemotion
 
ReactJS Vs React Native with 10 Significant Differences
Windzoon Technologies
 
React Native is Ready for Prime Time — Team × Technology (React Conf 2016)
James Ide
 
React Native Interview Questions.pptx
Gets Solution
 
A Closer Look At React Native
Ian Wang
 
Introduction to react native @ TIC NUST
Waqqas Jabbar
 
The Many Ways to Test Your React App
All Things Open
 
Which Tools Do React Native Developers Use to Create Interactive User Interfa...
vitaragaistechnolabs
 
[QE 2018] Adam Stasiak – Nadchodzi React Native – czyli o testowaniu mobilnyc...
Future Processing
 
Introduction to React Native
Waqqas Jabbar
 

More from Josh Justice (13)

PDF
Designing Effective Tests with React Testing Library - React Day Berlin 2022
Josh Justice
 
PDF
Building for Mobile and Web with Expo - React Day Berlin 2022
Josh Justice
 
PDF
Building for Mobile and Web with Expo - React Advanced London 2022
Josh Justice
 
PDF
Getting Better All the Time: How to Escape Bad Code
Josh Justice
 
PDF
Sustainable Learning - ReactATL Jan 2022
Josh Justice
 
PDF
Building an App for Mobile and Web with Expo
Josh Justice
 
PPTX
User-Modifiable Software: Smalltalk and HyperCard
Josh Justice
 
PDF
Practical Accessibility (A11y)
Josh Justice
 
PDF
Old Solutions to New Testing Problems
Josh Justice
 
PDF
Test-Driven Development in Vue with Cypress
Josh Justice
 
PDF
Test-Driven Development in React with Cypress
Josh Justice
 
PDF
Newbie's Guide to Contributing to Babel
Josh Justice
 
PPTX
Outside-in Testing in Vue with Cypress
Josh Justice
 
Designing Effective Tests with React Testing Library - React Day Berlin 2022
Josh Justice
 
Building for Mobile and Web with Expo - React Day Berlin 2022
Josh Justice
 
Building for Mobile and Web with Expo - React Advanced London 2022
Josh Justice
 
Getting Better All the Time: How to Escape Bad Code
Josh Justice
 
Sustainable Learning - ReactATL Jan 2022
Josh Justice
 
Building an App for Mobile and Web with Expo
Josh Justice
 
User-Modifiable Software: Smalltalk and HyperCard
Josh Justice
 
Practical Accessibility (A11y)
Josh Justice
 
Old Solutions to New Testing Problems
Josh Justice
 
Test-Driven Development in Vue with Cypress
Josh Justice
 
Test-Driven Development in React with Cypress
Josh Justice
 
Newbie's Guide to Contributing to Babel
Josh Justice
 
Outside-in Testing in Vue with Cypress
Josh Justice
 
Ad

Recently uploaded (20)

PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Human Resources Information System (HRIS)
Amity University, Patna
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Ad

Testing React Native Apps - Chain React 2023

  • 1. Testing React Native Apps As you get settled, write the answer to the following question on Post-It notes. Write as many answers on separates notes as you like. When you're done, put them on the back wall. What do you want to learn about testing in this workshop? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 2. Testing React Native Apps https://rnte.st/cr23 Clone the repo, build, and test! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 3. ReactNativeTesting.io Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 4. Josh Justice CodingItWrong.com Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 5. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 6. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 7. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 8. Thank you, assistants! — David Leuiliette — Jon Major Condon Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 9. Answer the following two questions on Post-It notes. Then discuss your answers with your group. Choose someone who will summarize your group's answers to the whole workshop afterward. 1. What benefits are you currently getting from testing? 2. What are your testing pain points? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 10. Why Test? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 11. My Story Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 12. Testing Pain Points — Difficult to test — Flaky tests — Fragile tests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 13. Example-Based Tests — Component tests (React Native Testing Library) — End-to-end tests (Detox) Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 14. Learning More Afterward — https://rnte.st/cr23 — Discord community Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 15. Questions and thoughts welcome! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 16. Stretch break! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 17. 02 Component Tests: Rendering Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 18. What is a component test? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 19. React Native Testing Library @testing-library/react-native callstack.github.io/react-native-testing-library Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 20. What to test? — Call methods/functions? — Check state values? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 21. "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 Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 22. Testing the Contract Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 23. "A component contract is the agreement between a component and the rest of the application." -- Edd Yerburgh, Testing Vue.js Applications Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 24. "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 Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 25. Discuss: What are some of the kinds of inputs and outputs that components have? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 26. Testing the Contract Inputs: — Props — User interaction events Outputs: — Rendered UI — Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 27. Testing the Contract Inputs: — Props — User interaction events Outputs: — Rendered UI — Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 28. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 29. Getter Functions — screen.getByText — screen.getByLabelText — screen.getByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 30. Text return <Text>Hello, world!</Text>; screen.getByText('Hello, world!') Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 31. TextInput return <TextInput placeholder="Enter your name" ... />; screen.getByPlaceholderText('Enter your name') Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 32. Image return ( <Image source={...} accessibilityLabel="squirrel waving" /> ); screen.getByLabelText('squirrel waving') Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 33. SVG import WavingHand from './assets/waving-hand.svg'; return <WavingHand accessibilityLabel="waving hand" />; screen.getByLabelText('waving hand') Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 34. Which Query Should I Use? 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" Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 35. Which Query Should I Use? callstack.github.io/react-native-testing-library/docs/how-should-i-query Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 36. Note on getByRole() screen.getByRole('button', {name: 'Save Changes'}) — May become the top recommendation soon — Feature needed first: implicit roles (Pressables queryable as button by default) Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 37. Confirming Presence expect(screen.getBy…()).toBeVisible(); expect(screen.getBy…()).toBeOnTheScreen(); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 38. Confirming Absence expect(screen.queryBy…()).not.toBeOnTheScreen(); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 39. Getter Functions — screen.getByText — screen.getByLabelText — screen.getByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 40. Query Functions — screen.queryByText — screen.queryByLabelText — screen.queryByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 41. Testing the Contract Inputs: — Props — User interaction events Outputs: — Rendered UI — Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 42. Testing the Contract Inputs: — Props — User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 43. Testing the Contract Inputs: — Props — User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 44. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 45. Testing the Contract Inputs: — Props — User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 46. Testing the Contract Inputs: ✅ Props - User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 47. Exercise 1 — rn-testing-exercises — exercises/Exercise1.md For help: reactnativetesting.io/component/testing/ Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 48. A few more notes about testing rendering and props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 49. Testing content, not appearance. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 50. What about snapshot tests? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 51. import renderer from 'react-test-renderer'; import Hello from './Hello'; it('renders correctly', () => { const tree = renderer.create(<Hello name="Josh" />).toJSON(); expect(tree).toMatchSnapshot(); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 52. 03 Component Tests: Actions and Mocks Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 53. Testing the Contract Inputs: ✅ Props - User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 54. Testing the Contract Inputs: ✅ Props - User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 55. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 56. import {fireEvent, render, screen} from '@testing-library/react-native'; describe('NewMessageForm', () => { describe('pressing send', () => { it('clears the message field', () => { render(<NewMessageForm />); fireEvent.changeText( screen.getByPlaceholderText('Message'), 'Hello world', ); fireEvent.press(screen.getByText('Send')); expect(screen.getByPlaceholderText('Message')).toHaveProp('value', ''); }); }); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 57. fireEvent import {fireEvent} from '@testing-library/react-native'; fireEvent.press(element) fireEvent.changeText(element, text) fireEvent.scroll(element, eventData) fireEvent(element, eventName, eventData) Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 58. import {render, fireEvent} from '@testing-library/react-native'; describe('NewMessageForm', () => { describe('pressing send', () => { it('clears the message field', () => { render(<NewMessageForm />); fireEvent.changeText( screen.getByPlaceholderText('Message'), 'Hello world', ); fireEvent.press(screen.getByText('Send')); expect(screen.getByPlaceholderText('Message')).toHaveProp('value', ''); }); }); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 59. toHaveProp Provided by Jest Native expect(...).toHaveProp('propName', 'propValue'); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 60. Testing the Contract Inputs: ✅ Props - User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 61. Testing the Contract Inputs: ✅ Props ✅ User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 62. Testing the Contract Inputs: ✅ Props ✅ User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 63. Mock Functions jestjs.io/docs/mock-functions Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 64. Creating a New Mock Function const myMock = jest.fn().mockName('myMock'); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 65. Mock Matchers expect(myMock).toHaveBeenCalled(); expect(myMock).toHaveBeenCalledWith(arg, secondArg, ...); expect(myMock).toHaveBeenCalledWith(expect.any(String)); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 66. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 67. it('calls the send handler', () => { const messageText = 'Hello world'; const sendHandler = jest.fn().mockName('sendHandler'); render(<NewMessageForm onSend={sendHandler} />); fireEvent.changeText( screen.getByPlaceholderText('Message'), messageText, ); fireEvent.press(screen.getByText('Send')); expect(sendHandler).toHaveBeenCalledWith(messageText); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 68. Question: Do you think mocks involve testing implementation details or not? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 69. My view: Mocks are an important part of testing the contract. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 70. Testing the Contract Inputs: ✅ Props ✅ User interaction events Outputs: ✅ Rendered UI - Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 71. Testing the Contract Inputs: ✅ Props ✅ User interaction events Outputs: ✅ Rendered UI ✅ Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 72. Exercise 2 — rn-testing-exercises — exercises/Exercise2.md For help: reactnativetesting.io/component/testing/#interaction Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 73. Testing the Contract Inputs: — Props — User interaction events Outputs: — Rendered UI — Calls to function props Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 74. 04 Component Tests: Effects and Module Mocks Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 75. How can we test effects? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 76. How can we test effects? Test the result/"effect". Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 77. Data loading effects hit the API Problem? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 78. Options for Mocking Web Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 79. Options for Mocking Web Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 80. Jest Module Mocks jestjs.io/docs/mock-functions#mocking-modules Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 81. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 82. import {render, screen} from '@testing-library/react-native'; import RestaurantContainer from './RestaurantContainer'; import api from './api'; jest.mock('./api'); describe('RestaurantContainer', () => { it('loads restaurants upon first render', async () => { api.get.mockResolvedValue({/* the response object */}); render(<RestaurantContainer />); expect(await screen.findByText('Pasta Place')).toBeVisible(); expect(screen.getByText('Salad Place')).toBeVisible(); expect(api.get).toHaveBeenCalledWith('/restaurants'); }); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 83. mockedModule.myfunc.mockReturnValue(val); mockedModule.myfunc.mockResolvedValue(val); mockedModule.myfunc.mockRejectedValue(val); jestjs.io/docs/mock-function-api Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 84. Getter Functions — screen.getByText — screen.getByLabelText — screen.getByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 85. Query Functions — screen.queryByText — screen.queryByLabelText — screen.queryByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 86. Find Functions — await screen.findByText — await screen.findByLabelText — await screen.findByPlaceholderText Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 87. Exercise 3 — rn-testing-exercises — exercises/Exercise3.md For help: reactnativetesting.io/component/testing Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 88. act() Error Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 89. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 90. Component Testing As a group, come up a way to explain what "testing the contract" of a component means to other developers. Use the poster in your area. You can write a bulleted list, use diagrams, draw a picture to use a metaphor, etc. Feel free to get creative! After, your group will present your board to the workshop. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 91. 05 End-to-End Testing: Basics Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 92. E2E Tests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 93. Choosing an E2E Test Library Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 94. — Runs on Hardware — Runs on Sauce Labs and AWS Device Farm — Runs completely outside your app — Have to use multiple libraries together — Hard to find complete documentation — Tests tend to be flaky Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 95. Detox — Write tests in JavaScript or TypeScript — Has inside knowledge of your app — Expo not officially supported — Can be complex to set up — Does not run on physical iOS devices yet Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 96. Maestro — Easy setup, a single library — Runs completely outside your app — Maestro Studio to interactively write commands — New; rough edges Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 97. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 98. it('detects text', async () => { await expect(element(by.label('Hello, Lecture 5!'))).toBeVisible(); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 99. element(…) Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 100. it('should have welcome screen', async () => { await expect( element(by.label('Welcome to React Native!')) ).toBeVisible(); }); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 101. Matchers by.id() by.text() by.label() Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 102. await expect(element(by.id('welcome'))) .toBeVisible(); await expect(element(by.id('welcome'))) .toHaveText('Welcome to React Native!'); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 103. Expectations toExist() toBeVisible() toHaveText() toHaveValue() Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 104. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 105. await element(by.id('mybutton')).tap(); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 106. await element(by.id('mytextinput')) .typeText('I typed this'); await element(by.id('mytextinput')) .clearText(); await element(by.id('mytextinput')) .typeText('Something else'); await element(by.id('mytextinput')) .replaceText('Something else'); Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 107. Actions tap() typeText() clearText() replaceText() scroll() swipe() Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 108. What to E2E test? Main paths, not all edge cases Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 109. 06 End-to-End Testing: External Services Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 110. Connecting to the real backend: Upsides? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 111. Connecting to the real backend: Downsides? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 112. Options for Mocking Web Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 113. Local web server? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 114. Options for Mocking Web Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 115. Fake Module Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 116. What's a fake? A version of the service that acts similar to the real thing but stores its data in-memory Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 117. To the code! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 118. API file setup api.js api.mock.js Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 119. api.js import axios from 'axios'; const api = axios.create({ baseURL: 'https://api.reactnativetesting.io', // other config }); export default api; Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 120. api.mock.js const api = { get() { return Promise.resolve({ data: [ {id: 1, name: 'Widget 1'}, {id: 2, name: 'Widget 2'}, ] }); }, // ... }; export default api; Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 121. Configure metro.config.js — Prefer .mock.js extension when present — Exact code may differ if you've modified metro.config.js for other reasons — E.g. using react-native-svg-transformer — See reactnativetesting.io/e2e/external-services Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 122. package.json "start": "react-native start", +"start:mock": "MOCK_API=true npm run start", "test": "jest" Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 123. For Windows support "start": "react-native start", +"start:mock": "cross-env MOCK_API=true npm run start", "test": "jest" Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 124. Exercise 4 — rn-testing-exercises — exercises/Exercise4.md For help: — reactnativetesting.io/e2e/testing Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 125. Recommendation: — Automate testing against a fake — Manually test integration with real service — If you need to automate testing with real service, do a small separate test suite Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 126. End-to-End Testing As a group, pick one of your group members' apps and come up with a list of the most valuable scenarios you would end-to-end test. Write each on a Post-It note and put it on your poster. If there are any you're uncertain how you would write a test for, discuss them as a group. Nominate someone to present your scenarios and questions to the whole workshop. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 127. 07 Wrap-up Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 128. How much of each type of test to do? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 129. Tradeoffs — End-to-End Tests: give confidence the whole app works — Unit and Component Tests: faster, easier, less flaky testing of edge cases, influence design of code Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 130. My Recommendations — End-to-end test main flows only — Component test edge cases — Extract plain functions and classes when it makes sense, and unit test them — Tweak as you go Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 131. To Discuss Write the answer to the following question on Post-It note. Use as many as you need: Based on what you learned today, what specifically do you want to do differently testing your React app going forward? Afterward we'll go around the room and each share our answers! Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 132. Remaining Questions? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 133. Remaining Questions? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 134. Continuing the Learning — https://rnte.st/cr23 — https://reactnativetesting.io — Discord community Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 135. Effective Detox Testing Oct 25, 2023 -Virtual — CI on GitHub Actions — Detox local development workflow — When Detox vs RNTL vs manual — How much to test with Detox Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 136. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 137. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 138. Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23