Home / Function/ patchConsoleForTestingBeforeHookInstallation() — react Function Reference

patchConsoleForTestingBeforeHookInstallation() — react Function Reference

Architecture documentation for the patchConsoleForTestingBeforeHookInstallation() function in setupTests.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  6688189a_a8d0_9baf_b39a_667bff44d4ea["patchConsoleForTestingBeforeHookInstallation()"]
  7a8b988c_a832_ec08_c36e_22d30113be12["setupTests.js"]
  6688189a_a8d0_9baf_b39a_667bff44d4ea -->|defined in| 7a8b988c_a832_ec08_c36e_22d30113be12
  3957f974_a9fb_cc07_c98c_c23acd5dea05["shouldIgnoreConsoleErrorOrWarn()"]
  6688189a_a8d0_9baf_b39a_667bff44d4ea -->|calls| 3957f974_a9fb_cc07_c98c_c23acd5dea05
  style 6688189a_a8d0_9baf_b39a_667bff44d4ea fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-devtools-shared/src/__tests__/setupTests.js lines 134–194

function patchConsoleForTestingBeforeHookInstallation() {
  const originalConsoleError = console.error;
  const originalConsoleWarn = console.warn;
  const originalConsoleLog = console.log;

  const consoleErrorMock = jest.fn();
  const consoleWarnMock = jest.fn();
  const consoleLogMock = jest.fn();

  global.consoleErrorMock = consoleErrorMock;
  global.consoleWarnMock = consoleWarnMock;
  global.consoleLogMock = consoleLogMock;

  console.error = (...args) => {
    let firstArg = args[0];
    if (typeof firstArg === 'string' && firstArg.startsWith('Warning: ')) {
      // Older React versions might use the Warning: prefix. I'm not sure
      // if they use this code path.
      firstArg = firstArg.slice(9);
    }
    if (firstArg === 'React instrumentation encountered an error: %o') {
      // Rethrow errors from React.
      throw args[1];
    } else if (
      typeof firstArg === 'string' &&
      (firstArg.startsWith("It looks like you're using the wrong act()") ||
        firstArg.startsWith(
          'The current testing environment is not configured to support act',
        ) ||
        firstArg.startsWith('You seem to have overlapping act() calls') ||
        firstArg.startsWith(
          'ReactDOM.render is no longer supported in React 18.',
        ))
    ) {
      // DevTools intentionally wraps updates with acts from both DOM and test-renderer,
      // since test updates are expected to impact both renderers.
      return;
    } else if (shouldIgnoreConsoleErrorOrWarn(args)) {
      // Allows testing how DevTools behaves when it encounters console.error without cluttering the test output.
      // Errors can be ignored by running in a special context provided by utils.js#withErrorsOrWarningsIgnored
      return;
    }

    consoleErrorMock(...args);
    originalConsoleError.apply(console, args);
  };
  console.warn = (...args) => {
    if (shouldIgnoreConsoleErrorOrWarn(args)) {
      // Allows testing how DevTools behaves when it encounters console.warn without cluttering the test output.
      // Warnings can be ignored by running in a special context provided by utils.js#withErrorsOrWarningsIgnored
      return;
    }

    consoleWarnMock(...args);
    originalConsoleWarn.apply(console, args);
  };
  console.log = (...args) => {
    consoleLogMock(...args);
    originalConsoleLog.apply(console, args);
  };
}

Domain

Subdomains

Frequently Asked Questions

What does patchConsoleForTestingBeforeHookInstallation() do?
patchConsoleForTestingBeforeHookInstallation() is a function in the react codebase, defined in packages/react-devtools-shared/src/__tests__/setupTests.js.
Where is patchConsoleForTestingBeforeHookInstallation() defined?
patchConsoleForTestingBeforeHookInstallation() is defined in packages/react-devtools-shared/src/__tests__/setupTests.js at line 134.
What does patchConsoleForTestingBeforeHookInstallation() call?
patchConsoleForTestingBeforeHookInstallation() calls 1 function(s): shouldIgnoreConsoleErrorOrWarn.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free