-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bdd react native
232 lines (183 loc) · 4.96 KB
/
Bdd react native
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
Setting up a React Native project with TypeScript, Cucumber, and Detox involves several steps. Here’s a comprehensive guide including the folder structure, files, and example code for a login test.
### Folder Structure
```
my-react-native-app/
├── .detox/
├── android/
├── ios/
├── e2e/
│ ├── step_definitions/
│ │ └── loginSteps.ts
│ ├── support/
│ │ └── world.ts
│ └── login.feature
├── src/
│ ├── components/
│ ├── screens/
│ │ └── LoginScreen.tsx
│ ├── App.tsx
│ └── index.ts
├── .buckconfig
├── .eslintrc.js
├── .gitignore
├── babel.config.js
├── cucumber.js
├── detox.config.js
├── package.json
├── tsconfig.json
└── yarn.lock
```
### 1. Set Up Your Project
1. **Initialize your React Native project with TypeScript:**
```bash
npx react-native init my-react-native-app --template react-native-template-typescript
cd my-react-native-app
```
2. **Install necessary dependencies:**
```bash
yarn add cucumber detox @cucumber/cucumber detox-cli @types/cucumber
```
3. **Install Detox dependencies:**
```bash
yarn add detox --dev
```
4. **Install TypeScript and related types:**
```bash
yarn add typescript @types/react @types/react-native --dev
```
### 2. Configure TypeScript
Ensure your `tsconfig.json` is set up correctly:
```json
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"module": "commonjs",
"moduleResolution": "node",
"jsx": "react",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*", "e2e/**/*"],
"exclude": ["node_modules"]
}
```
### 3. Configure Detox
Create a `detox.config.js` file:
```js
module.exports = {
testRunner: 'jest',
runnerConfig: 'e2e/config.json',
configurations: {
android: {
device: {
avdName: 'Pixel_3a_API_30_x86',
type: 'android.emulator',
},
app: 'android/app/build/outputs/apk/debug/app-debug.apk',
},
ios: {
device: {
type: 'simulator',
},
app: 'ios/build/Build/Products/Debug-iphonesimulator/my-react-native-app.app',
},
},
};
```
### 4. Configure Cucumber
Create a `cucumber.js` file:
```js
module.exports = {
default: `--require-module ts-node/register --require e2e/step_definitions/**/*.ts --format progress --format json:./reports/cucumber_report.json`,
};
```
### 5. Detox Configuration for Jest
Create `e2e/config.json`:
```json
{
"setupFilesAfterEnv": ["<rootDir>/e2e/setup.js"],
"testTimeout": 120000
}
```
Create `e2e/setup.js`:
```js
const detox = require('detox');
const config = require('../detox.config');
beforeAll(async () => {
await detox.init(config);
}, 30000);
afterAll(async () => {
await detox.cleanup();
});
```
### 6. Example Code
#### `e2e/login.feature`
```gherkin
Feature: Login
Scenario: Successful login
Given I am on the login screen
When I enter valid credentials
And I tap on the login button
Then I should see the home screen
```
#### `e2e/step_definitions/loginSteps.ts`
```typescript
import { Given, When, Then } from '@cucumber/cucumber';
import { element, by, expect } from 'detox';
Given('I am on the login screen', async () => {
await expect(element(by.id('loginScreen'))).toBeVisible();
});
When('I enter valid credentials', async () => {
await element(by.id('usernameInput')).typeText('testuser');
await element(by.id('passwordInput')).typeText('password123');
});
When('I tap on the login button', async () => {
await element(by.id('loginButton')).tap();
});
Then('I should see the home screen', async () => {
await expect(element(by.id('homeScreen'))).toBeVisible();
});
```
#### `src/screens/LoginScreen.tsx`
```typescript
import React from 'react';
import { View, TextInput, Button, StyleSheet } from 'react-native';
const LoginScreen = () => {
return (
<View testID="loginScreen" style={styles.container}>
<TextInput testID="usernameInput" placeholder="Username" style={styles.input} />
<TextInput testID="passwordInput" placeholder="Password" secureTextEntry style={styles.input} />
<Button testID="loginButton" title="Login" onPress={() => {}} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 16,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 12,
paddingHorizontal: 8,
},
});
export default LoginScreen;
```
### 7. Running Your Tests
To run your Detox tests, use:
```bash
npx detox test --configuration ios
```
or
```bash
npx detox test --configuration android
```
Ensure your emulator or simulator is running before executing the tests.
This setup provides a complete guide for configuring Cucumber and Detox with a React Native TypeScript project and includes an example login test.