Skip to content

Commit 9c9bccf

Browse files
authored
Fix to RCTTestRunner so that RedBox errors are logged when piped thru xcpretty. (facebook#18)
Fix to RNTesterApp so that setState is not called after the component is unmounted causing warnings that can fail integration tests.
1 parent e23623c commit 9c9bccf

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Libraries/RCTTest/RCTTestRunner.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName
199199
if (expectErrorBlock) {
200200
RCTAssert(expectErrorBlock(errors[0]), @"Expected an error but the first one was missing or did not match.");
201201
} else {
202-
RCTAssert(errors == nil, @"RedBox errors: %@", errors);
202+
// [TODO(OSS Candidate ISS#2710739): xcpretty formats the test failure output to show only one line of the assert string followed by a snippet of source code including the assert statement and the lines just before and after.
203+
// Convert the `errors` array into a single line string delimited by \n so that CI logs contain meaningful information.
204+
RCTAssert(errors == nil, @"RedBox errors: %@", [[errors valueForKey:@"description"] componentsJoinedByString:@"\\n"]); // ]TODO(OSS Candidate ISS#2710739)
203205
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %0.f seconds", kTestTimeoutSeconds);
204206
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
205207
}

Libraries/WebSocket/WebSocket.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
224224
}
225225

226226
_close(code?: number, reason?: string): void {
227-
if (Platform.OS === 'android' || Platform.OS === 'win32' || Platform.OS == 'windesktop') {
227+
if (
228+
Platform.OS === 'android' ||
229+
Platform.OS === 'win32' ||
230+
Platform.OS === 'windesktop'
231+
) {
228232
// See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
229233
const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL;
230234
const closeReason = typeof reason === 'string' ? reason : '';

RNTester/js/RNTesterApp.ios.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,21 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => (
5858
);
5959

6060
class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
61+
_mounted: boolean; // TODO(OSS Candidate ISS#2710739)
62+
6163
UNSAFE_componentWillMount() {
6264
BackHandler.addEventListener('hardwareBackPress', this._handleBack);
6365
}
6466

6567
componentDidMount() {
68+
this._mounted = true; // TODO(OSS Candidate ISS#2710739)
6669
Linking.getInitialURL().then(url => {
6770
AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => {
71+
// [TODO(OSS Candidate ISS#2710739)
72+
if (!this._mounted) {
73+
return;
74+
}
75+
// ]TODO(OSS Candidate ISS#2710739)
6876
const exampleAction = URIActionMap(
6977
this.props.exampleFromAppetizeParams,
7078
);
@@ -89,6 +97,12 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
8997
});
9098
}
9199

100+
// [TODO(OSS Candidate ISS#2710739)
101+
componentWillUnmount() {
102+
this._mounted = false;
103+
}
104+
// ]TODO(OSS Candidate ISS#2710739)
105+
92106
_handleBack = () => {
93107
this._handleAction(RNTesterActions.Back());
94108
};

RNTester/js/RNTesterApp.macos.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => (
6060
);
6161

6262
class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
63+
_mounted: boolean; // TODO(OSS Candidate ISS#2710739)
64+
6365
UNSAFE_componentWillMount() {
6466
// BackHandler.addEventListener('hardwareBackPress', this._handleBack);
6567
}
6668

6769
componentDidMount() {
70+
this._mounted = true; // TODO(OSS Candidate ISS#2710739)
6871
Linking.getInitialURL().then(url => {
6972
AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => {
73+
// [TODO(OSS Candidate ISS#2710739)
74+
if (!this._mounted) {
75+
return;
76+
}
77+
// ]TODO(OSS Candidate ISS#2710739)
7078
const exampleAction = URIActionMap(
7179
this.props.exampleFromAppetizeParams,
7280
);
@@ -91,6 +99,12 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
9199
});
92100
}
93101

102+
// [TODO(OSS Candidate ISS#2710739)
103+
componentWillUnmount() {
104+
this._mounted = false;
105+
}
106+
// ]TODO(OSS Candidate ISS#2710739)
107+
94108
_handleBack = () => {
95109
this._handleAction(RNTesterActions.Back());
96110
};

0 commit comments

Comments
 (0)