Skip to content

Commit d5e03b9

Browse files
committed
fix(runtime): guard LogBox native module on web
Avoid crashing the web playground before Harness can report ready when react-native-web does not expose TurboModuleRegistry.
1 parent 08fc575 commit d5e03b9

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
const importLogBox = async () => {
4+
vi.resetModules();
5+
return await import('../logbox.js');
6+
};
7+
8+
describe('disableLogBoxUI', () => {
9+
beforeEach(() => {
10+
vi.restoreAllMocks();
11+
vi.doUnmock('react-native');
12+
});
13+
14+
it('does not require TurboModuleRegistry on web', async () => {
15+
const ignoreAllLogs = vi.fn();
16+
17+
vi.doMock('react-native', () => ({
18+
LogBox: {
19+
ignoreAllLogs,
20+
},
21+
TurboModuleRegistry: undefined,
22+
}));
23+
24+
const { disableLogBoxUI, isLogBoxSuppressed } = await importLogBox();
25+
26+
expect(() => disableLogBoxUI()).not.toThrow();
27+
expect(ignoreAllLogs).toHaveBeenCalledWith(true);
28+
expect(isLogBoxSuppressed()).toBe(true);
29+
});
30+
31+
it('hides the native LogBox module when available', async () => {
32+
const nativeLogBox = {
33+
show: vi.fn(),
34+
hide: vi.fn(),
35+
};
36+
37+
vi.doMock('react-native', () => ({
38+
LogBox: {
39+
ignoreAllLogs: vi.fn(),
40+
},
41+
TurboModuleRegistry: {
42+
get: vi.fn(() => nativeLogBox),
43+
},
44+
}));
45+
46+
const { disableLogBoxUI } = await importLogBox();
47+
48+
disableLogBoxUI();
49+
50+
expect(nativeLogBox.show()).toBeUndefined();
51+
expect(nativeLogBox.hide()).toBeUndefined();
52+
});
53+
});

packages/runtime/src/logbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const disableLogBoxUI = (): void => {
3030
harnessLogBox.addLog = noop;
3131
harnessLogBox.addConsoleLog = noop;
3232

33-
const nativeLogBox = TurboModuleRegistry.get<NativeLogBoxModule>('LogBox');
33+
const nativeLogBox = TurboModuleRegistry?.get<NativeLogBoxModule>('LogBox');
3434
if (nativeLogBox != null) {
3535
nativeLogBox.show = noop;
3636
nativeLogBox.hide = noop;

0 commit comments

Comments
 (0)