mirror of
https://github.com/status-im/react-native.git
synced 2025-01-13 19:15:05 +00:00
Make RCTTestRunner wait for JS context to deallocate
This commit is contained in:
parent
44fec06891
commit
9b1f6c9e30
@ -16,13 +16,8 @@
|
|||||||
#import "RCTTestModule.h"
|
#import "RCTTestModule.h"
|
||||||
#import "RCTUtils.h"
|
#import "RCTUtils.h"
|
||||||
|
|
||||||
#define TIMEOUT_SECONDS 60
|
static const NSTimeInterval kTestTimeoutSeconds = 60;
|
||||||
|
static const NSTimeInterval kTestTeardownTimeoutSeconds = 30;
|
||||||
@interface RCTBridge (RCTTestRunner)
|
|
||||||
|
|
||||||
@property (nonatomic, weak) RCTBridge *batchedBridge;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation RCTTestRunner
|
@implementation RCTTestRunner
|
||||||
{
|
{
|
||||||
@ -49,7 +44,7 @@
|
|||||||
_scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
|
_scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
|
||||||
RCTAssert(_scriptURL != nil, @"Could not locate main.jsBundle");
|
RCTAssert(_scriptURL != nil, @"Could not locate main.jsBundle");
|
||||||
#else
|
#else
|
||||||
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?dev=true&platform=ios", app]];
|
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", app]];
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
@ -83,52 +78,69 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|||||||
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
||||||
initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
|
initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
|
||||||
{
|
{
|
||||||
__block NSString *error = nil;
|
__weak id weakJSContext;
|
||||||
RCTSetLogFunction(^(RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
|
||||||
if (level >= RCTLogLevelError) {
|
@autoreleasepool {
|
||||||
error = message;
|
__block NSString *error = nil;
|
||||||
|
RCTSetLogFunction(^(RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
||||||
|
if (level >= RCTLogLevelError) {
|
||||||
|
error = message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
|
||||||
|
moduleProvider:_moduleProvider
|
||||||
|
launchOptions:nil];
|
||||||
|
|
||||||
|
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProps];
|
||||||
|
rootView.frame = CGRectMake(0, 0, 320, 2000); // Constant size for testing on multiple devices
|
||||||
|
|
||||||
|
NSString *testModuleName = RCTBridgeModuleNameForClass([RCTTestModule class]);
|
||||||
|
RCTTestModule *testModule = rootView.bridge.modules[testModuleName];
|
||||||
|
RCTAssert(_testController != nil, @"_testController should not be nil");
|
||||||
|
testModule.controller = _testController;
|
||||||
|
testModule.testSelector = test;
|
||||||
|
testModule.view = rootView;
|
||||||
|
|
||||||
|
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
|
||||||
|
vc.view = [UIView new];
|
||||||
|
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
|
||||||
|
|
||||||
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kTestTimeoutSeconds];
|
||||||
|
while (date.timeIntervalSinceNow > 0 && testModule.status == RCTTestStatusPending && error == nil) {
|
||||||
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
||||||
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
|
// Take a weak reference to the JS context, so we track its deallocation later
|
||||||
moduleProvider:_moduleProvider
|
// (we can only do this now, since it's been lazily initialized)
|
||||||
launchOptions:nil];
|
weakJSContext = [[[bridge valueForKey:@"batchedBridge"] valueForKey:@"javaScriptExecutor"] valueForKey:@"context"];
|
||||||
|
[rootView removeFromSuperview];
|
||||||
|
|
||||||
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProps];
|
RCTSetLogFunction(RCTDefaultLogFunction);
|
||||||
rootView.frame = CGRectMake(0, 0, 320, 2000); // Constant size for testing on multiple devices
|
|
||||||
|
|
||||||
NSString *testModuleName = RCTBridgeModuleNameForClass([RCTTestModule class]);
|
NSArray *nonLayoutSubviews = [vc.view.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id subview, NSDictionary *bindings) {
|
||||||
RCTTestModule *testModule = rootView.bridge.batchedBridge.modules[testModuleName];
|
return ![NSStringFromClass([subview class]) isEqualToString:@"_UILayoutGuide"];
|
||||||
RCTAssert(_testController != nil, @"_testController should not be nil");
|
}]];
|
||||||
testModule.controller = _testController;
|
RCTAssert(nonLayoutSubviews.count == 0, @"There shouldn't be any other views: %@", nonLayoutSubviews);
|
||||||
testModule.testSelector = test;
|
|
||||||
testModule.view = rootView;
|
|
||||||
|
|
||||||
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
|
if (expectErrorBlock) {
|
||||||
vc.view = [UIView new];
|
RCTAssert(expectErrorBlock(error), @"Expected an error but nothing matched.");
|
||||||
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
|
} else {
|
||||||
|
RCTAssert(error == nil, @"RedBox error: %@", error);
|
||||||
|
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %0.f seconds", kTestTimeoutSeconds);
|
||||||
|
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
|
||||||
|
}
|
||||||
|
[bridge invalidate];
|
||||||
|
}
|
||||||
|
|
||||||
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
|
// Wait for the executor to have shut down completely before returning
|
||||||
while (date.timeIntervalSinceNow > 0 && testModule.status == RCTTestStatusPending && error == nil) {
|
NSDate *teardownTimeout = [NSDate dateWithTimeIntervalSinceNow:kTestTeardownTimeoutSeconds];
|
||||||
|
while (teardownTimeout.timeIntervalSinceNow > 0 && weakJSContext) {
|
||||||
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
||||||
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
||||||
}
|
}
|
||||||
[rootView removeFromSuperview];
|
RCTAssert(!weakJSContext, @"JS context was not deallocated after being invalidated");
|
||||||
|
|
||||||
RCTSetLogFunction(RCTDefaultLogFunction);
|
|
||||||
|
|
||||||
NSArray *nonLayoutSubviews = [vc.view.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id subview, NSDictionary *bindings) {
|
|
||||||
return ![NSStringFromClass([subview class]) isEqualToString:@"_UILayoutGuide"];
|
|
||||||
}]];
|
|
||||||
RCTAssert(nonLayoutSubviews.count == 0, @"There shouldn't be any other views: %@", nonLayoutSubviews);
|
|
||||||
|
|
||||||
if (expectErrorBlock) {
|
|
||||||
RCTAssert(expectErrorBlock(error), @"Expected an error but nothing matched.");
|
|
||||||
} else {
|
|
||||||
RCTAssert(error == nil, @"RedBox error: %@", error);
|
|
||||||
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %d seconds", TIMEOUT_SECONDS);
|
|
||||||
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
#import "RCTAssert.h"
|
#import "RCTAssert.h"
|
||||||
#import "RCTBridge.h"
|
#import "RCTBridge.h"
|
||||||
#import "RCTContextExecutor.h"
|
|
||||||
#import "RCTEventDispatcher.h"
|
#import "RCTEventDispatcher.h"
|
||||||
#import "RCTKeyCommands.h"
|
#import "RCTKeyCommands.h"
|
||||||
#import "RCTLog.h"
|
#import "RCTLog.h"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user