This repository was archived by the owner on Feb 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTests.tsx
More file actions
229 lines (212 loc) · 5.85 KB
/
Tests.tsx
File metadata and controls
229 lines (212 loc) · 5.85 KB
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
import React, { Component } from 'react';
import {
Button,
Platform,
StyleSheet,
Text,
View,
NativeModules,
} from 'react-native';
import { Client } from 'mocha-remote-client';
import { Runner } from 'mocha';
// Registering an error handler that always throw unhandled exceptions
// This is to enable the remote-mocha-cli to exit on uncaught errors
// const originalHandler = ErrorUtils.getGlobalHandler();
// ErrorUtils.setGlobalHandler((err, isFatal) => {
// // Calling the original handler to show the error visually too
// originalHandler(err, isFatal);
// throw err;
// });
const engine = (global as any).HermesInternal ? 'hermes' : 'jsc';
class Tests extends Component {
state = {
status: 'disconnected',
totalTests: 0,
currentTest: '',
currentTestIndex: 0,
failures: 0,
reason: '',
};
client?: Client;
runner?: Runner;
componentDidMount(): void {
this.prepareTests();
}
componentWillUnmount(): void {
if (this.client) {
console.log('Disconnecting from the mocha-remote server');
this.client.disconnect();
}
}
render(): React.ReactNode {
const { totalTests, currentTestIndex, status } = this.state;
const progress = totalTests > 0 ? currentTestIndex / totalTests : 0;
return (
<View style={styles.container}>
<Text style={styles.status}>{this.statusMessage}</Text>
<Text style={styles.status}>
progress:{' '}
{progress === 1 || progress === 0 ? progress : progress.toFixed(2)}
</Text>
<Text style={styles.details}>{this.statusDetails}</Text>
<Button
title="Run tests natively"
disabled={status === 'running'}
onPress={this.handleRerunNative}
/>
<Button
title="Abort running the tests"
disabled={status !== 'running'}
onPress={this.handleAbort}
/>
</View>
);
}
handleRerunNative = (): void => {
NativeModules.DevSettings.reload();
};
handleAbort = (): void => {
if (this.runner) {
this.runner.abort();
}
};
get statusMessage(): string | null {
if (this.state.status === 'disconnected') {
return 'Disconnected from mocha-remote-server';
} else if (this.state.status === 'waiting') {
return 'Waiting for server to start tests';
} else if (this.state.status === 'running') {
return 'Running the tests';
} else if (this.state.status === 'ended') {
return 'The tests ended';
} else {
return null;
}
}
get statusDetails(): string | null {
const { status, currentTest, currentTestIndex, failures, reason } =
this.state;
if (status === 'running') {
return currentTest;
} else if (typeof reason === 'string') {
return reason;
} else if (typeof failures === 'number') {
return `Ran ${currentTestIndex + 1} tests (${failures} failures)`;
} else {
return null;
}
}
get statusColor(): string | undefined {
const { status, failures } = this.state;
if (status === 'ended') {
return failures > 0 ? 'red' : 'green';
} else {
return undefined;
}
}
prepareTests(): void {
this.client = new Client({
title: `React-Native on ${Platform.OS} (using ${engine})`,
tests: (context): void => {
/* eslint-env mocha */
// Adding an async hook before each test to allow the UI to update
beforeEach(function () {
// WFT it doesn't work ?
return new Promise((resolve) => setTimeout(resolve, 10));
});
// global.fs = require("react-native-fs");
// global.path = require("path-browserify");
// global.environment = {
// Default to the host machine when running on Android
// realmBaseUrl: Platform.OS === "android" ? "http://10.0.2.2:9090" : undefined,
// ...context,
// reactNative: Platform.OS,
// android: Platform.OS === "android",
// ios: Platform.OS === "ios",
// };
(global as any).environment = JSON.parse(
Buffer.from(context.c as string, 'hex').toString('utf-8'),
);
// Make the tests reinitializable, to allow test running on changes to the "realm" package
// Probing the existance of `getModules` as this only exists in debug mode
// if ("getModules" in require) {
// const modules = require.getModules();
// for (const [, m] of Object.entries(modules)) {
// if (m.verboseName.startsWith("../../tests/")) {
// m.isInitialized = false;
// }
// }
// }
// Require in the integration tests
require('./tests');
},
});
this.client
// @ts-ignore
.on('connected', () => {
console.log('Connected to mocha-remote-server');
this.setState({ status: 'waiting' });
})
// @ts-ignore
.on('disconnected', ({ reason = 'No reason' }) => {
console.error(`Disconnected: ${reason}`);
this.setState({ status: 'disconnected', reason });
})
.on('running', (runner) => {
// Store the active runner on the App
this.runner = runner;
// Check if the tests were loaded correctly
if (runner.total > 0) {
this.setState({
status: 'running',
failures: 0,
currentTestIndex: 0,
totalTests: runner.total,
});
} else {
this.setState({
status: 'ended',
reason: 'No tests were loaded',
});
}
runner.on('test', (test) => {
// Compute the current test index - incrementing it if we're running
// Set the state to update the UI
this.setState({
status: 'running',
currentTest: test.fullTitle(),
currentTestIndex: this.state.currentTestIndex + 1,
totalTests: runner.total,
});
});
runner.on('end', () => {
this.setState({
status: 'ended',
failures: runner.failures,
});
delete this.client;
delete this.runner;
});
});
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
status: {
fontSize: 20,
margin: 10,
},
details: {
fontSize: 14,
padding: 10,
width: '100%',
textAlign: 'center',
height: 100,
},
});
export default Tests;