Skip to content

Commit

Permalink
Wait for bridge to disappear when running tests
Browse files Browse the repository at this point in the history
Reviewed By: fkgozali

Differential Revision: D5592347

fbshipit-source-id: c9a672f2d79c8656b72f585aac7c6f5fec6e72b0
  • Loading branch information
javache authored and facebook-github-bot committed Aug 9, 2017
1 parent b78b8cc commit b1bb0a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Libraries/RCTTest/RCTTestRunner.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "RCTTestRunner.h"

#import <React/RCTAssert.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTLog.h>
#import <React/RCTRootView.h>
#import <React/RCTUtils.h>
Expand Down Expand Up @@ -94,20 +95,23 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName
configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock
expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
{
__weak RCTBridge *batchedBridge;

@autoreleasepool {
__block NSString *error = nil;
RCTLogFunction defaultLogFunction = RCTGetLogFunction();
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
defaultLogFunction(level, source, fileName, lineNumber, message);
if (level >= RCTLogLevelError) {
error = message;
} else {
defaultLogFunction(level, source, fileName, lineNumber, message);
}
});

RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
moduleProvider:_moduleProvider
launchOptions:nil];
batchedBridge = [bridge batchedBridge];


RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProps];
#if TARGET_OS_TV
Expand Down Expand Up @@ -156,8 +160,17 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %0.f seconds", kTestTimeoutSeconds);
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
}

[bridge invalidate];
}

// Wait for bridge to disappear before continuing to the next test
NSDate *invalidateTimeout = [NSDate dateWithTimeIntervalSinceNow:5];
while (invalidateTimeout.timeIntervalSinceNow > 0 && batchedBridge != nil) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
RCTAssert(batchedBridge == nil, @"Bridge should be deallocated after the test");
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ - (void)testTheTester_ExpectError
RCT_TEST(WebViewTest)
#endif


@end

0 comments on commit b1bb0a7

Please sign in to comment.