mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
5eddf1d79a
Summary: This diff adds error handling to logbox so that if there is an error either when parsing logs or when rendering LogBox, we show a native redbox with the error that was thrown and a message explaining that it's an internal React Native error. Changelog: [Internal] Reviewed By: cpojer Differential Revision: D18394788 fbshipit-source-id: 5d74d58e4b28ef6d863079e83677fb23ef4ccb34
139 lines
3.4 KiB
JavaScript
139 lines
3.4 KiB
JavaScript
/**
|
||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
*
|
||
* This source code is licensed under the MIT license found in the
|
||
* LICENSE file in the root directory of this source tree.
|
||
*
|
||
* @emails oncall+react_native
|
||
* @format
|
||
* @flow
|
||
*/
|
||
|
||
'use strict';
|
||
|
||
const LogBox = require('../LogBox');
|
||
const LogBoxData = require('../Data/LogBoxData');
|
||
|
||
declare var console: any;
|
||
|
||
describe('LogBox', () => {
|
||
const {error, warn} = console;
|
||
|
||
beforeEach(() => {
|
||
jest.resetModules();
|
||
console.error = jest.fn();
|
||
console.warn = jest.fn();
|
||
});
|
||
|
||
afterEach(() => {
|
||
LogBox.uninstall();
|
||
console.error = error;
|
||
console.warn = warn;
|
||
});
|
||
|
||
it('can set `disableLogBox` after installing', () => {
|
||
expect(console.disableLogBox).toBe(undefined);
|
||
|
||
LogBox.install();
|
||
|
||
expect(console.disableLogBox).toBe(false);
|
||
expect(LogBoxData.isDisabled()).toBe(false);
|
||
|
||
console.disableLogBox = true;
|
||
|
||
expect(console.disableLogBox).toBe(true);
|
||
expect(LogBoxData.isDisabled()).toBe(true);
|
||
});
|
||
|
||
it('can set `disableLogBox` before installing', () => {
|
||
expect(console.disableLogBox).toBe(undefined);
|
||
|
||
console.disableLogBox = true;
|
||
LogBox.install();
|
||
|
||
expect(console.disableLogBox).toBe(true);
|
||
expect(LogBoxData.isDisabled()).toBe(true);
|
||
});
|
||
|
||
it('registers warnings', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
|
||
LogBox.install();
|
||
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
console.warn('...');
|
||
expect(LogBoxData.addLog).toBeCalled();
|
||
});
|
||
|
||
it('reports a LogBox exception if we fail to add warnings', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
const mockError = new Error('Simulated error');
|
||
|
||
// Picking a random implemention detail to simulate throwing.
|
||
(LogBoxData.isMessageIgnored: any).mockImplementation(() => {
|
||
throw mockError;
|
||
});
|
||
|
||
LogBox.install();
|
||
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
expect(LogBoxData.reportLogBoxError).not.toBeCalled();
|
||
console.warn('...');
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
expect(LogBoxData.reportLogBoxError).toBeCalledWith(mockError);
|
||
});
|
||
|
||
it('registers errors beginning with "Warning: " as warnings', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
|
||
LogBox.install();
|
||
|
||
console.error('...');
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
|
||
console.error('Warning: ...');
|
||
expect(LogBoxData.addLog).toBeCalledWith({
|
||
category: 'Warning: ...',
|
||
componentStack: [],
|
||
level: 'warn',
|
||
message: {content: 'Warning: ...', substitutions: []},
|
||
});
|
||
});
|
||
|
||
it('ignores logs that are pattern ignored"', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
(LogBoxData.isMessageIgnored: any).mockReturnValue(true);
|
||
|
||
LogBox.install();
|
||
|
||
console.warn('ignored message');
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
});
|
||
|
||
it('ignores logs starting with "(ADVICE)"', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
|
||
LogBox.install();
|
||
|
||
console.warn('(ADVICE) ...');
|
||
expect(LogBoxData.addLog).not.toBeCalled();
|
||
});
|
||
|
||
it('does not ignore logs formatted to start with "(ADVICE)"', () => {
|
||
jest.mock('../Data/LogBoxData');
|
||
|
||
LogBox.install();
|
||
|
||
console.warn('%s ...', '(ADVICE)');
|
||
expect(LogBoxData.addLog).toBeCalledWith({
|
||
category: '%s ...',
|
||
componentStack: [],
|
||
level: 'warn',
|
||
message: {
|
||
content: '(ADVICE) ...',
|
||
substitutions: [{length: 8, offset: 0}],
|
||
},
|
||
});
|
||
});
|
||
});
|