Repository: facebook/react
File: packages/react-devtools-shared/src/__tests__/setupTests.js (JavaScript)

Code before:
  jest.useFakeTimers();

  // Use utils.js#withErrorsOrWarningsIgnored instead of directly mutating this array.
  global._ignoredErrorOrWarningMessages = [];
  function shouldIgnoreConsoleErrorOrWarn(args) {
    let firstArg = args[0];
    if (
      firstArg !== null &&
      typeof firstArg === 'object' &&
      String(firstArg).indexOf('Error: Uncaught [') === 0
    ) {
      firstArg = String(firstArg);
    } else if (typeof firstArg !== 'string') {
      return false;
    }
    const shouldFilter = global._ignoredErrorOrWarningMessages.some(
      errorOrWarningMessage => {
        return firstArg.indexOf(errorOrWarningMessage) !== -1;
      },
    );

    return shouldFilter;
  }

  const originalConsoleError = console.error;
  // $FlowFixMe
  console.error = (...args) => {
    const firstArg = args[0];
    if (
      firstArg === 'Warning: React instrumentation encountered an error: %s'
    ) {
      // Rethrow errors from React.
      throw args[1];
    } else if (
      typeof firstArg === 'string' &&
      (firstArg.startsWith(
        "Warning: It looks like you're using the wrong act()",
      ) ||
        firstArg.startsWith(
          'Warning: The current testing environment is not configured to support act',
        ) ||
        firstArg.startsWith(
          'Warning: You seem to have overlapping act() calls',
        ))
    ) {
      // 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

Review comment (bug):
Is there a flowconfig to fail on unused suppressions? Would be nice to continuously remove unused suppressions instead of having to remove them manually.

Code after:
  jest.useFakeTimers();

  // Use utils.js#withErrorsOrWarningsIgnored instead of directly mutating this array.
  global._ignoredErrorOrWarningMessages = [];
  function shouldIgnoreConsoleErrorOrWarn(args) {
    let firstArg = args[0];
    if (
      firstArg !== null &&
      typeof firstArg === 'object' &&
      String(firstArg).indexOf('Error: Uncaught [') === 0
    ) {
      firstArg = String(firstArg);
    } else if (typeof firstArg !== 'string') {
      return false;
    }
    const shouldFilter = global._ignoredErrorOrWarningMessages.some(
      errorOrWarningMessage => {
        return firstArg.indexOf(errorOrWarningMessage) !== -1;
      },
    );

    return shouldFilter;
  }

  const originalConsoleError = console.error;
  console.error = (...args) => {
    const firstArg = args[0];
    if (
      firstArg === 'Warning: React instrumentation encountered an error: %s'
    ) {
      // Rethrow errors from React.
      throw args[1];
    } else if (
      typeof firstArg === 'string' &&
      (firstArg.startsWith(
        "Warning: It looks like you're using the wrong act()",
      ) ||
        firstArg.startsWith(
          'Warning: The current testing environment is not configured to support act',
        ) ||
        firstArg.startsWith(
          'Warning: You seem to have overlapping act() calls',
        ))
    ) {
      // 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;