DevTools: Patch console methods even when only show-inline-warnings/errors enabled (#20688)
Brian Vaughn committed
Feb 2, 2021 at 10:19 UTC
3e0bdbefa3cb78c5534b808772ed6afad7c34827
4 files changed
+66
-3
packages/react-devtools-shared/src/__tests__/console-test.js
+51
@@ -47,6 +47,7 @@ describe('console', () => {
47
patchConsole({
48
appendComponentStack: true,
49
breakOnWarn: false,
50
+ showInlineWarningsAndErrors: false,
51
});
52
53
const inject = global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject;
@@ -79,12 +80,61 @@ describe('console', () => {
80
expect(fakeConsole.warn).not.toBe(mockWarn);
81
});
82
83
+ it('should patch the console when appendComponentStack is enabled', () => {
84
+ unpatchConsole();
85
+
86
+ expect(fakeConsole.error).toBe(mockError);
87
+ expect(fakeConsole.warn).toBe(mockWarn);
88
+
89
+ patchConsole({
90
+ appendComponentStack: true,
91
+ breakOnWarn: false,
92
+ showInlineWarningsAndErrors: false,
93
+ });
94
+
95
+ expect(fakeConsole.error).not.toBe(mockError);
96
+ expect(fakeConsole.warn).not.toBe(mockWarn);
97
+ });
98
+
99
+ it('should patch the console when breakOnWarn is enabled', () => {
100
+ unpatchConsole();
101
+
102
+ expect(fakeConsole.error).toBe(mockError);
103
+ expect(fakeConsole.warn).toBe(mockWarn);
104
+
105
+ patchConsole({
106
+ appendComponentStack: false,
107
+ breakOnWarn: true,
108
+ showInlineWarningsAndErrors: false,
109
+ });
110
+
111
+ expect(fakeConsole.error).not.toBe(mockError);
112
+ expect(fakeConsole.warn).not.toBe(mockWarn);
113
+ });
114
+
115
+ it('should patch the console when showInlineWarningsAndErrors is enabled', () => {
116
+ unpatchConsole();
117
+
118
+ expect(fakeConsole.error).toBe(mockError);
119
+ expect(fakeConsole.warn).toBe(mockWarn);
120
+
121
+ patchConsole({
122
+ appendComponentStack: false,
123
+ breakOnWarn: false,
124
+ showInlineWarningsAndErrors: true,
125
+ });
126
+
127
+ expect(fakeConsole.error).not.toBe(mockError);
128
+ expect(fakeConsole.warn).not.toBe(mockWarn);
129
+ });
130
+
131
it('should only patch the console once', () => {
132
const {error, warn} = fakeConsole;
133
134
patchConsole({
135
appendComponentStack: true,
136
breakOnWarn: false,
137
+ showInlineWarningsAndErrors: false,
138
});
139
140
expect(fakeConsole.error).toBe(error);
@@ -339,6 +389,7 @@ describe('console', () => {
389
patchConsole({
390
appendComponentStack: true,
391
breakOnWarn: false,
392
+ showInlineWarningsAndErrors: false,
393
});
394
act(() => ReactDOM.render(<Child />, document.createElement('div')));
395
packages/react-devtools-shared/src/backend/agent.js
+5
-1
@@ -622,7 +622,11 @@ export default class Agent extends EventEmitter<{|
622
// or in the case of React Native- if the backend is just finding out the preference-
623
// then install or uninstall the console overrides.
624
// It's safe to call these methods multiple times, so we don't need to worry about that.
625
- if (appendComponentStack || breakOnConsoleErrors) {
625
+ if (
626
+ appendComponentStack ||
627
+ breakOnConsoleErrors ||
628
+ showInlineWarningsAndErrors
629
+ ) {
630
patchConsole({
631
appendComponentStack,
632
breakOnConsoleErrors,
packages/react-devtools-shared/src/backend/renderer.js
+5
-1
@@ -645,7 +645,11 @@ export function attach(
645
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ === true;
646
const showInlineWarningsAndErrors =
647
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
648
- if (appendComponentStack || breakOnConsoleErrors) {
648
+ if (
649
+ appendComponentStack ||
650
+ breakOnConsoleErrors ||
651
+ showInlineWarningsAndErrors
652
+ ) {
653
patchConsole({
654
appendComponentStack,
655
breakOnConsoleErrors,
packages/react-devtools-shared/src/hook.js
+5
-1
@@ -192,7 +192,11 @@ export function installHook(target: any): DevToolsHook | null {
192
// but Webpack wraps imports with an object (e.g. _backend_console__WEBPACK_IMPORTED_MODULE_0__)
193
// and the object itself will be undefined as well for the reasons mentioned above,
194
// so we use try/catch instead.
195
- if (appendComponentStack || breakOnConsoleErrors) {
195
+ if (
196
+ appendComponentStack ||
197
+ breakOnConsoleErrors ||
198
+ showInlineWarningsAndErrors
199
+ ) {
200
registerRendererWithConsole(renderer);
201
patchConsole({
202
appendComponentStack,