6

I'm trying to suppress a specific error in our Jest tests as we've got plans to resolve it later, but until then we don't want these errors potentially hiding any true errors.

The errors occur across the codebase so my current strategy is to rewire the console.error function in our jest setup file.

I've taken two different attempts, both almost working however now when they report a true error - they're adding a new line onto the error trace.

Attempt 1:

const originalError = console.error.bind(console.error);
jest.spyOn(global.console, 'error').mockImplementation((message) => {
  if (
    !message
      .toString()
      .includes(
        'Warning: An update to SettingsPanel inside a test was not wrapped in act'
      )
  ) {
    originalError(message);
  }
});

Attempt 2:

const { error } = console;
global.console = {
  ...console,
  error: (errorMessage) =>
    !errorMessage
      .toString()
      .includes(
        'Warning: An update to SettingsPanel inside a test was not wrapped in act'
      ) && error(errorMessage),
};

Both of these will log a valid error - i.e. 'Warning: Each child in a list should have a unique "key"' - However instead of pointing me to the code with the issue it points me back here

console.error
    Warning: Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/docs/lists-and-keys.html#keys for more information.%s

      18 |       .includes(
      19 |         'Warning: An update to SettingsPanel inside a test was not wrapped in act'
    > 20 |       ) && error(errorMessage),
         |            ^
      21 | };

      at Object.error (jest.setup.js:20:12)
      at printWarning (node_modules/react/cjs/react.development.js:315:30)
      at error (node_modules/react/cjs/react.development.js:287:5)
      at validateExplicitKey (node_modules/react/cjs/react.development.js:1630:5)
      at validateChildKeys (node_modules/react/cjs/react.development.js:1656:9)
      at Object.createElementWithValidation [as createElement] (node_modules/react/cjs/react.development.js:1806:7)
      at src/js/components/SettingsPanel/DataListAutocompleter/DataListAutocompleter.tsx:85:13

I've searched and searched and all I can find are questions about silencing all errors or silencing a specific test - nothing I've found has helped me solve this.

0

1 Answer 1

0

Use failOnConsole in your setupFilesAfterEnv configuration.

You can configure this to silence some errors, and fail on others:

import failOnConsole from 'jest-fail-on-console'

failOnConsole({
  silenceMessage: (errorMessage) => {
    if (/'Warning: An update to .*? inside a test was not wrapped in act/.test(errorMessage)) {
      return true
    }
    return false
  },
})

This should silence ALL instances of that error. Everything else will go through, it will give you more lines, but should still preserve the original source of the error, underneath the line it adds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.