-
Notifications
You must be signed in to change notification settings - Fork 24.4k
/
RCTLoggingTests.m
144 lines (115 loc) · 5.58 KB
/
RCTLoggingTests.m
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <XCTest/XCTest.h>
#import <React/RCTAssert.h>
#import <React/RCTBridge.h>
#import <React/RCTConstants.h>
#import <React/RCTLog.h>
// Time to wait for an expected log statement to show before failing the test
const int64_t LOGGER_TIMEOUT = 10 * NSEC_PER_SEC;
@interface RCTLoggingTests : XCTestCase
@end
@implementation RCTLoggingTests {
RCTBridge *_bridge;
dispatch_semaphore_t _logSem;
RCTLogLevel _lastLogLevel;
RCTLogSource _lastLogSource;
NSString *_lastLogMessage;
}
- (void)setUp
{
NSURL *scriptURL;
if (getenv("CI_USE_PACKAGER")) {
NSString *app = @"IntegrationTests/IntegrationTestsApp";
scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=%@&dev=true",
app,
RCTPlatformName]];
} else {
scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
}
RCTAssert(scriptURL != nil, @"No scriptURL set");
_bridge = [[RCTBridge alloc] initWithBundleURL:scriptURL moduleProvider:NULL launchOptions:nil];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60];
while (date.timeIntervalSinceNow > 0 && _bridge.loading) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
XCTAssertFalse(_bridge.loading);
_logSem = dispatch_semaphore_create(0);
// Set the log function to signal the semaphore
RCTSetLogFunction(
^(RCTLogLevel level,
RCTLogSource source,
__unused NSString *fileName,
__unused NSNumber *lineNumber,
NSString *message) {
if (source == RCTLogSourceJavaScript) {
self->_lastLogLevel = level;
self->_lastLogSource = source;
self->_lastLogMessage = message;
dispatch_semaphore_signal(self->_logSem);
}
});
}
- (void)tearDown
{
[_bridge invalidate];
_bridge = nil;
RCTSetLogFunction(RCTDefaultLogFunction);
}
- (void)testLogging
{
intptr_t waitRet = 0;
// First queue the marker and spin until it happens to be logged.
// This is to ensure we skip all of the other messages, that were logged earlier.
NSString *const LogMarker = @"===LOG_MARKER===";
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsole" args:@[ LogMarker ]];
do {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for log marker");
} while (waitRet == 0 && ![_lastLogMessage isEqualToString:LogMarker]);
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsole" args:@[ @"Invoking console.log" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logToConsole");
XCTAssertEqual(_lastLogLevel, RCTLogLevelInfo);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.log");
[_bridge enqueueJSCall:@"LoggingTestModule.warning" args:@[ @"Generating warning" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for warning");
XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Generating warning");
[_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[ @"Invariant failed" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for invariant");
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertTrue([_lastLogMessage containsString:@"Invariant Violation: Invariant failed"]);
[_bridge enqueueJSCall:@"LoggingTestModule.logErrorToConsole" args:@[ @"Invoking console.error" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logErrorToConsole");
// For local bundles, we may first get a warning about symbolication
if (![_lastLogMessage isEqualToString:@"Invoking console.error"]) {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for logErrorToConsole #2");
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.error");
[_bridge enqueueJSCall:@"LoggingTestModule.throwError" args:@[ @"Throwing an error" ]];
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for throwError");
// For local bundles, we may first get a warning about symbolication
if (![_lastLogMessage containsString:@"Error: Throwing an error"]) {
waitRet = dispatch_semaphore_wait(_logSem, dispatch_time(DISPATCH_TIME_NOW, LOGGER_TIMEOUT));
XCTAssertEqual(waitRet, 0, @"Timed out waiting for throwError #2");
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertTrue([_lastLogMessage containsString:@"Error: Throwing an error"]);
}
@end