Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LogBox] Add LogBox.isIgnoredLog() for expo remote logging integration #34476

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Libraries/LogBox/LogBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface ILogBox {
isInstalled(): boolean;
ignoreLogs($ReadOnlyArray<IgnorePattern>): void;
ignoreAllLogs(?boolean): void;
isIgnoredLog(...args: Array<mixed>): boolean;
clearAllLogs(): void;
addLog(log: LogData): void;
addException(error: ExtendedExceptionData): void;
Expand Down Expand Up @@ -111,6 +112,18 @@ if (__DEV__) {
LogBoxData.setDisabled(value == null ? true : value);
},

isIgnoredLog(...args: Array<mixed>): boolean {
if (LogBoxData.isDisabled()) {
return true;
}

try {
const {message} = parseLogBoxLog(args);
return LogBoxData.isMessageIgnored(message.content);
} catch (err) {}
return false;
},

clearAllLogs(): void {
LogBoxData.clear();
},
Expand Down Expand Up @@ -244,6 +257,10 @@ if (__DEV__) {
// Do nothing.
},

isIgnoredLog(...args: Array<mixed>): boolean {
return false;
},

clearAllLogs(): void {
// Do nothing.
},
Expand Down
42 changes: 42 additions & 0 deletions Libraries/LogBox/__tests__/LogBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,46 @@ describe('LogBox', () => {
'Custom: after installing for the second time',
);
});

it('`isIgnoredLog` returns true for ignored log', () => {
jest.unmock('../Data/LogBoxData');
LogBox.ignoreLogs(['ignoreMe']);
expect(LogBox.isIgnoredLog('ignoreMe: message', 'another argument')).toBe(
true,
);
});

it('`isIgnoredLog` returns true for regexp based ignored log', () => {
jest.unmock('../Data/LogBoxData');
LogBox.ignoreLogs([/ignore\d*Me/]);
expect(LogBox.isIgnoredLog('ignoreMe')).toBe(true);
expect(LogBox.isIgnoredLog('ignore123Me')).toBe(true);
});

it('`isIgnoredLog` returns true for any messages when `ignoreAllLogs` was called', () => {
jest.unmock('../Data/LogBoxData');
LogBox.ignoreAllLogs();
expect(LogBox.isIgnoredLog('any messsages')).toBe(true);
});

it('`isIgnoredLog` returns false for non-matched ignored log', () => {
jest.unmock('../Data/LogBoxData');
LogBox.ignoreLogs(['ignoreMe']);
expect(LogBox.isIgnoredLog('showMe: message', 'another argument')).toBe(
false,
);
});

it('`isIgnoredLog` returns false when throwing exception from log parser.', () => {
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.ignoreLogs(['ignoreMe']);
expect(LogBox.isIgnoredLog('ignoreMe')).toBe(false);
});
});