KEMBAR78
Testing React Native Apps - Chain React 2023 | PDF
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

Testing React Native Apps - Chain React 2023

  • 1.
    Testing React NativeApps 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 NativeApps 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 NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 4.
    Josh Justice CodingItWrong.com Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 5.
    Testing React NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 6.
    Testing React NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 7.
    Testing React NativeApps - 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 followingtwo 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 ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 11.
    My Story Testing ReactNative 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 — Componenttests (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! TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 16.
    Stretch break! Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 17.
    02 Component Tests: Rendering TestingReact Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 18.
    What is acomponent test? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 19.
    React Native TestingLibrary @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 encouragereading 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 TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 23.
    "A component contractis 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 canassume 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 someof 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! TestingReact 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="Enteryour 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 ShouldI 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 ShouldI 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.
  • 38.
    Confirming Absence expect(screen.queryBy…()).not.toBeOnTheScreen(); Testing ReactNative 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! TestingReact 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 morenotes 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. TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 50.
    What about snapshottests? 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: Actionsand 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! TestingReact 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 JestNative 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 ✅ Userinteraction 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 ✅ Userinteraction 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 ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 64.
    Creating a NewMock 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! TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 67.
    it('calls the sendhandler', () => { 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 thinkmocks 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 arean 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 ✅ Userinteraction 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 ✅ Userinteraction 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: Effectsand Module Mocks Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 75.
    How can wetest effects? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 76.
    How can wetest effects? Test the result/"effect". Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 77.
    Data loading effectshit the API Problem? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 78.
    Options for MockingWeb Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 79.
    Options for MockingWeb 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 TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 81.
    To the code! TestingReact 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.
  • 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 — awaitscreen.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 ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 89.
    To the code! TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 90.
    Component Testing As agroup, 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 TestingReact Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 92.
    E2E Tests Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 93.
    Choosing an E2ETest Library Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 94.
    — Runs onHardware — 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 testsin 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! TestingReact 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 NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 100.
    it('should have welcomescreen', 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 NativeApps - 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('Welcometo 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 NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 104.
    To the code! TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 105.
    await element(by.id('mybutton')).tap(); Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 106.
    await element(by.id('mytextinput')) .typeText('I typedthis'); 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 NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 108.
    What to E2Etest? 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: ExternalServices Testing React Native Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 110.
    Connecting to thereal backend: Upsides? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 111.
    Connecting to thereal backend: Downsides? Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 112.
    Options for MockingWeb Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 113.
    Local web server? TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 114.
    Options for MockingWeb Service Requests Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 115.
    Fake Module Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 116.
    What's a fake? Aversion 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! TestingReact Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 118.
    API file setup api.js api.mock.js TestingReact 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 testingagainst 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 agroup, 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 ReactNative Apps Testing React Native Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 128.
    How much ofeach 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-endtest 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 theanswer 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 ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 133.
    Remaining Questions? Testing ReactNative 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 Oct25, 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 NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 137.
    Testing React NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 138.
    Testing React NativeApps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23
  • 139.
    Thanks! https://rnte.st/cr23survey Josh Justice josh.justice@testdouble.com https://rnte.st/cr23 Testing ReactNative Apps - Josh Justice - Chain React - 2023-05-17 - https://rnte.st/cr23