Effective Detox Testing - React Advanced 2023

JoshJustice1 119 views 89 slides Oct 24, 2023
Slide 1
Slide 1 of 89
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89

About This Presentation

So you’ve gotten Detox set up to test your React Native application. Good work! But you aren’t done yet: there are still a lot of questions you need to answer. How many tests do you write? When and where do you run them? How do you ensure there is test data available? What do you do about parts ...


Slide Content

Effective Detox Testing
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Overview
1.What kinds of things to test with Detox
2.Faking the API layer
3.How much of your app to test with Detox
4.Detox on CI
5.Local Detox development workflow
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Prerequisites
—Experience with React Native
—Experience with Detox
—Experience with different types of tests: unit,
component, end-to-end
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Josh Justice
CodingItWrong.com
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

ReactNativeTesting.io
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

https://rnte.st/london23
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

1. What kinds of things to
test with Detox
…vs. what kinds of things to test
with other tools
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Kinds of Testing
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

"Testing is all about
confidence."
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Realism != Confidence
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Realism like…
—Get a push notification
—OS alert prompts you to apply an OS update
—Network connection drops
—Virtual keyboard appears/disappears when you don't
expect
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

A less-realistic, stable test
gives you more confidence than
a realistic, flaky test.
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

A less-realistic test you run
all the time
gives you more confidence than
a realistic test you don't run
because it's slow.
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Testing Goals
—Realism
—Speed
—Reliability
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Manual Testing
—Animation smoothness
—User experience
—Integration with third-party
services
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

End-to-End Testing
—Integration of first-party and
third-party JS and native code
—Can the user do what they
want to do?
—Can the app make money?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Component Testing
—Fully cover logical branches in
UI code
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Unit Testing
—Fully cover logical branches in
non-UI code
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

The more realistic the test,
the more effort it is to write
and maintain it.
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Test Pyramid
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Test Pyramid
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Questions?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

2. Faking the API Layer
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Connecting to the real backend:
Upsides?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Connecting to the real backend:
Downsides?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

App Layers
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Options for Mocking Web Service Requests
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Local web server?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Options for Mocking Web Service Requests
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Fake Module
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

What's a fake?
A version of the service that acts similar to the real thing
but stores its data in-memory
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

To the code!
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

API file setup
api.js
api.mock.js
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

api.js
import axios from 'axios';
const api = axios.create({
baseURL: 'https://api.reactnativetesting.io' ,
// other config
});
export default api;
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

api.mock.js
const api = {
get() {
return Promise.resolve({
data: [
{id: 1, name: 'Widget 1'},
{id: 2, name: 'Widget 2'},
]
});
},
// ...
};
export default api;
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Configure metro.config.js
—When flag is set, 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
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

package.json
"start": "react-native start",
+"start:mock": "MOCK_API=true npm run start",
"test": "jest"
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

For Windows support
"start": "react-native start",
+"start:mock": "cross-env MOCK_API=true npm run start",
"test": "jest"
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Questions?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

3. How Much to Test with
Detox
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

To the code!
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Types of Detox Test (not rigid categories)
—Boot test
—Play-around test
—The most important moneymaking feature, happy
path flow
—Secondary feature happy paths
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Factors
—Flake
—Test speed
—Ease of writing
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Types of Detox Test (not rigid categories)
—Boot test
—Play-around test
—The most important moneymaking feature, happy
path flow
—Secondary feature happy paths
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Questions?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

4. Detox on CI
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

CI for native mobile apps is
hard
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

1. Variations between CI
Platforms
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

2. Dependent on details of
specific versions
On GitHub Actions, Detox works for me on macos-12
and times out on macos-13
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

3. Slow
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

4. Expensive if you pay by the
hour
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

5. If you have a suite that passes
on developer machines, it's hard
to get it all passing on CI at once
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

My GitHub Actions Detox
CI Setup
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

To the YAML!
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

name: Detox
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

jobs:
test:
runs-on: macos-12
steps:
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
cache: "yarn"
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

- name: Cache Pods dependencies
uses: actions/cache@v3
with:
path: ios/Pods
key: ${{ runner.OS }}-pods-cache-${{ hashFiles('**/ios/Podfile.lock') }}
restore-keys: |
${{ runner.OS }}-pods-cache-
- name: Install npm dependencies
run: yarn install --frozen-lockfile
- name: Install iOS dependencies
run: npx pod-install
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

- name: Install Detox CLI
run: |
brew tap wix/brew
brew install applesimutils
npm install -g detox-cli
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

- name: Build App for Detox
run: detox build -c ios.sim.release
- uses: futureware-tech/simulator-action@v2
with:
model: "iPhone 14"
- name: Run Detox
run: detox test -c ios.sim.release
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Recommendation
—Pick your potential CI platform
—Try to get a boot test passing on CI
—If it doesn't work, try another platform
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

5. Local Detox development
workflow
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

3 Stages
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Stage 1: Test Existing
Features
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Stage 2: Test Features As
You Add
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Stage 3: Test-Driven
Development
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Getting Better All
the Time: How to
Escape Bad Code
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

To the code!
learntdd.in/react-native
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Why TDD?
—Thorough test coverage.
—Code that is easy to test.
—Minimal code.
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

3 Stages
1.Test some existing features
2.Test new features as you add them
3.Test-driven development
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Questions?
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

What We Covered
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

1. What kinds of things to test with Detox
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

1. What kinds of things to test with Detox
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

2. Faking the API layer
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

3. How much to test with Detox
—Boot test
—Play-around test
—The most important moneymaking feature, happy
path flow
—Secondary feature happy paths
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

4. Detox on CI
GitHub Actions example config
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

5. Stages of Detox development
1.Test some existing features
2.Test new features as you add them
3.Test-driven development
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

What We Covered
1.What kinds of things to test with Detox
2.Faking the API layer
3.How much of your app to test with Detox
4.Detox on CI
5.Local Detox development workflow
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Thanks!
Questions?
Josh Justice
[email protected]
https://rnte.st/london23
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23

Thanks!
Josh Justice
[email protected]
https://rnte.st/london23
Effective Detox Testing - Josh Justice - React Advanced - 2023-10-25 - https://rnte.st/london23