Skip to content

Commit

Permalink
DevTools: Patch console methods even when only show-inline-warnings/e…
Browse files Browse the repository at this point in the history
…rrors enabled (#20688)
  • Loading branch information
Brian Vaughn authored Feb 2, 2021
1 parent 7cb9fd7 commit 3e0bdbe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
51 changes: 51 additions & 0 deletions packages/react-devtools-shared/src/__tests__/console-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('console', () => {
patchConsole({
appendComponentStack: true,
breakOnWarn: false,
showInlineWarningsAndErrors: false,
});

const inject = global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject;
Expand Down Expand Up @@ -79,12 +80,61 @@ describe('console', () => {
expect(fakeConsole.warn).not.toBe(mockWarn);
});

it('should patch the console when appendComponentStack is enabled', () => {
unpatchConsole();

expect(fakeConsole.error).toBe(mockError);
expect(fakeConsole.warn).toBe(mockWarn);

patchConsole({
appendComponentStack: true,
breakOnWarn: false,
showInlineWarningsAndErrors: false,
});

expect(fakeConsole.error).not.toBe(mockError);
expect(fakeConsole.warn).not.toBe(mockWarn);
});

it('should patch the console when breakOnWarn is enabled', () => {
unpatchConsole();

expect(fakeConsole.error).toBe(mockError);
expect(fakeConsole.warn).toBe(mockWarn);

patchConsole({
appendComponentStack: false,
breakOnWarn: true,
showInlineWarningsAndErrors: false,
});

expect(fakeConsole.error).not.toBe(mockError);
expect(fakeConsole.warn).not.toBe(mockWarn);
});

it('should patch the console when showInlineWarningsAndErrors is enabled', () => {
unpatchConsole();

expect(fakeConsole.error).toBe(mockError);
expect(fakeConsole.warn).toBe(mockWarn);

patchConsole({
appendComponentStack: false,
breakOnWarn: false,
showInlineWarningsAndErrors: true,
});

expect(fakeConsole.error).not.toBe(mockError);
expect(fakeConsole.warn).not.toBe(mockWarn);
});

it('should only patch the console once', () => {
const {error, warn} = fakeConsole;

patchConsole({
appendComponentStack: true,
breakOnWarn: false,
showInlineWarningsAndErrors: false,
});

expect(fakeConsole.error).toBe(error);
Expand Down Expand Up @@ -339,6 +389,7 @@ describe('console', () => {
patchConsole({
appendComponentStack: true,
breakOnWarn: false,
showInlineWarningsAndErrors: false,
});
act(() => ReactDOM.render(<Child />, document.createElement('div')));

Expand Down
6 changes: 5 additions & 1 deletion packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,11 @@ export default class Agent extends EventEmitter<{|
// or in the case of React Native- if the backend is just finding out the preference-
// then install or uninstall the console overrides.
// It's safe to call these methods multiple times, so we don't need to worry about that.
if (appendComponentStack || breakOnConsoleErrors) {
if (
appendComponentStack ||
breakOnConsoleErrors ||
showInlineWarningsAndErrors
) {
patchConsole({
appendComponentStack,
breakOnConsoleErrors,
Expand Down
6 changes: 5 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ export function attach(
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ === true;
const showInlineWarningsAndErrors =
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
if (appendComponentStack || breakOnConsoleErrors) {
if (
appendComponentStack ||
breakOnConsoleErrors ||
showInlineWarningsAndErrors
) {
patchConsole({
appendComponentStack,
breakOnConsoleErrors,
Expand Down
6 changes: 5 additions & 1 deletion packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ export function installHook(target: any): DevToolsHook | null {
// but Webpack wraps imports with an object (e.g. _backend_console__WEBPACK_IMPORTED_MODULE_0__)
// and the object itself will be undefined as well for the reasons mentioned above,
// so we use try/catch instead.
if (appendComponentStack || breakOnConsoleErrors) {
if (
appendComponentStack ||
breakOnConsoleErrors ||
showInlineWarningsAndErrors
) {
registerRendererWithConsole(renderer);
patchConsole({
appendComponentStack,
Expand Down

0 comments on commit 3e0bdbe

Please sign in to comment.