Properly handle LogBox errors during tests

Summary:
This diff fixes an issue where errors in LogBox during tests would cause the tests to crash.

The crash is due to the NativeExceptionsManager module not being mocked (as all native module need to be in tests). The fix is to properly mock the NativeExceptionManger.

This fix exposed an infinite loop issue where failures in LogBox will be logged to the ExceptionManager, which logs to the console, which logs to LogBox, creating a loop. This diff also fixes that look by moving the LogBox internal error check to the top of the monkey patched console methods.

Changelog: [Internal]

Differential Revision: D20428590

fbshipit-source-id: 7289a480c99ba8dee67772178b7629afb40b330a
This commit is contained in:
Rick Hanlon 2020-03-19 20:29:05 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 5a3c6faee9
Коммит 9c64bd5739
2 изменённых файлов: 30 добавлений и 17 удалений

Просмотреть файл

@ -102,6 +102,12 @@ if (__DEV__) {
};
const registerWarning = (...args): void => {
// Let warnings within LogBox itself fall through.
if (LogBoxData.isLogBoxErrorMessage(String(args[0]))) {
error.call(console, ...args);
return;
}
try {
if (!isRCTLogAdviceWarning(...args)) {
const {category, message, componentStack} = parseLogBoxLog(args);
@ -110,14 +116,12 @@ if (__DEV__) {
// Be sure to pass LogBox warnings through.
warn.call(console, ...args);
if (!LogBoxData.isLogBoxErrorMessage(message.content)) {
LogBoxData.addLog({
level: 'warn',
category,
message,
componentStack,
});
}
LogBoxData.addLog({
level: 'warn',
category,
message,
componentStack,
});
}
}
} catch (err) {
@ -126,6 +130,12 @@ if (__DEV__) {
};
const registerError = (...args): void => {
// Let errors within LogBox itself fall through.
if (LogBoxData.isLogBoxErrorMessage(args[0])) {
error.call(console, ...args);
return;
}
try {
if (!isWarningModuleWarning(...args)) {
// Only show LogBox for the `warning` module, otherwise pass through and skip.
@ -156,15 +166,12 @@ if (__DEV__) {
const interpolated = parseInterpolation(args);
error.call(console, interpolated.message.content);
// Only display errors outside of LogBox, not in LogBox itself.
if (!LogBoxData.isLogBoxErrorMessage(message.content)) {
LogBoxData.addLog({
level,
category,
message,
componentStack,
});
}
LogBoxData.addLog({
level,
category,
message,
componentStack,
});
}
} catch (err) {
LogBoxData.reportLogBoxError(err);

Просмотреть файл

@ -35,6 +35,12 @@ jest.setMock(
jest
.mock('../Libraries/Core/InitializeCore', () => {})
.mock('../Libraries/Core/NativeExceptionsManager', () => ({
__esModule: true,
default: {
reportException: jest.fn(),
},
}))
.mock('../Libraries/ReactNative/UIManager', () => ({
AndroidViewPager: {
Commands: {