Skip to content

Commit 333ca38

Browse files
committed
refactor[react-devtools]: move console patching to global hook
1 parent b521ef8 commit 333ca38

File tree

13 files changed

+670
-1383
lines changed

13 files changed

+670
-1383
lines changed

packages/react-devtools-core/src/backend.js

-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Agent from 'react-devtools-shared/src/backend/agent';
1111
import Bridge from 'react-devtools-shared/src/bridge';
1212
import {installHook} from 'react-devtools-shared/src/hook';
1313
import {initBackend} from 'react-devtools-shared/src/backend';
14-
import {installConsoleFunctionsToWindow} from 'react-devtools-shared/src/backend/console';
1514
import {__DEBUG__} from 'react-devtools-shared/src/constants';
1615
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
1716
import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils';
@@ -41,9 +40,6 @@ type ConnectOptions = {
4140
devToolsSettingsManager: ?DevToolsSettingsManager,
4241
};
4342

44-
// Install a global variable to allow patching console early (during injection).
45-
// This provides React Native developers with components stacks even if they don't run DevTools.
46-
installConsoleFunctionsToWindow();
4743
installHook(window);
4844

4945
const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

packages/react-devtools-inline/src/backend.js

-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import Agent from 'react-devtools-shared/src/backend/agent';
44
import Bridge from 'react-devtools-shared/src/bridge';
55
import {initBackend} from 'react-devtools-shared/src/backend';
6-
import {installConsoleFunctionsToWindow} from 'react-devtools-shared/src/backend/console';
76
import {installHook} from 'react-devtools-shared/src/hook';
87
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
98

@@ -120,8 +119,5 @@ export function createBridge(contentWindow: any, wall?: Wall): BackendBridge {
120119
}
121120

122121
export function initialize(contentWindow: any): void {
123-
// Install a global variable to allow patching console early (during injection).
124-
// This provides React Native developers with components stacks even if they don't run DevTools.
125-
installConsoleFunctionsToWindow();
126122
installHook(contentWindow);
127123
}

packages/react-devtools-shared/src/__tests__/componentStacks-test.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@
77
* @flow
88
*/
99

10-
import {getVersionedRenderImplementation, normalizeCodeLocInfo} from './utils';
10+
import {
11+
getVersionedRenderImplementation,
12+
normalizeCodeLocInfo,
13+
} from 'react-devtools-shared/src/__tests__/utils';
1114

1215
describe('component stack', () => {
1316
let React;
1417
let act;
15-
let mockError;
16-
let mockWarn;
1718
let supportsOwnerStacks;
1819

1920
beforeEach(() => {
20-
// Intercept native console methods before DevTools bootstraps.
21-
// Normalize component stack locations.
22-
mockError = jest.fn();
23-
mockWarn = jest.fn();
24-
console.error = (...args) => {
25-
mockError(...args.map(normalizeCodeLocInfo));
26-
};
27-
console.warn = (...args) => {
28-
mockWarn(...args.map(normalizeCodeLocInfo));
29-
};
30-
3121
const utils = require('./utils');
3222
act = utils.act;
3323

@@ -54,18 +44,22 @@ describe('component stack', () => {
5444

5545
act(() => render(<Grandparent />));
5646

57-
expect(mockError).toHaveBeenCalledWith(
47+
expect(
48+
global.consoleErrorMock.mock.calls[0].map(normalizeCodeLocInfo),
49+
).toEqual([
5850
'Test error.',
5951
'\n in Child (at **)' +
6052
'\n in Parent (at **)' +
6153
'\n in Grandparent (at **)',
62-
);
63-
expect(mockWarn).toHaveBeenCalledWith(
54+
]);
55+
expect(
56+
global.consoleWarnMock.mock.calls[0].map(normalizeCodeLocInfo),
57+
).toEqual([
6458
'Test warning.',
6559
'\n in Child (at **)' +
6660
'\n in Parent (at **)' +
6761
'\n in Grandparent (at **)',
68-
);
62+
]);
6963
});
7064

7165
// This test should have caught #19911
@@ -89,13 +83,15 @@ describe('component stack', () => {
8983

9084
expect(useEffectCount).toBe(1);
9185

92-
expect(mockWarn).toHaveBeenCalledWith(
86+
expect(
87+
global.consoleWarnMock.mock.calls[0].map(normalizeCodeLocInfo),
88+
).toEqual([
9389
'Warning to trigger appended component stacks.',
9490
'\n in Example (at **)',
95-
);
91+
]);
9692
});
9793

98-
// @reactVersion >=18.3
94+
// @reactVersion >= 18.3
9995
it('should log the current component stack with debug info from promises', () => {
10096
const Child = () => {
10197
console.error('Test error.');
@@ -117,23 +113,27 @@ describe('component stack', () => {
117113

118114
act(() => render(<Grandparent />));
119115

120-
expect(mockError).toHaveBeenCalledWith(
116+
expect(
117+
global.consoleErrorMock.mock.calls[0].map(normalizeCodeLocInfo),
118+
).toEqual([
121119
'Test error.',
122120
supportsOwnerStacks
123121
? '\n in Child (at **)'
124122
: '\n in Child (at **)' +
125-
'\n in ServerComponent (at **)' +
126-
'\n in Parent (at **)' +
127-
'\n in Grandparent (at **)',
128-
);
129-
expect(mockWarn).toHaveBeenCalledWith(
123+
'\n in ServerComponent (at **)' +
124+
'\n in Parent (at **)' +
125+
'\n in Grandparent (at **)',
126+
]);
127+
expect(
128+
global.consoleWarnMock.mock.calls[0].map(normalizeCodeLocInfo),
129+
).toEqual([
130130
'Test warning.',
131131
supportsOwnerStacks
132132
? '\n in Child (at **)'
133133
: '\n in Child (at **)' +
134-
'\n in ServerComponent (at **)' +
135-
'\n in Parent (at **)' +
136-
'\n in Grandparent (at **)',
137-
);
134+
'\n in ServerComponent (at **)' +
135+
'\n in Parent (at **)' +
136+
'\n in Grandparent (at **)',
137+
]);
138138
});
139139
});

0 commit comments

Comments
 (0)