ValentinH / jest-fail-on-console

Utility to make jest tests fail when console.error() or any other methods are used
MIT License
141 stars 20 forks source link

silenceMessage isn't throwing on some errors #3

Closed notthatnathan closed 2 years ago

notthatnathan commented 3 years ago
  silenceMessage: (errorMessage, methodName) => {
    if (/test was not wrapped in act/.test(errorMessage)) {
      return true;
    }

    if (
      /It looks like you're using a version of react-dom that supports the "act" function/.test(
        errorMessage,
      )
    ) {
      return true;
    }

    console.log('errorMessage', methodName, errorMessage);
    return false;
  },
    errorMessage error Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
Test Suites: 1 passed, 1 total
Tests:       18 passed, 18 total

This successfully silences the errors I'm excepting by returning true. But it's not throwing on the other console.error instances in the same component.

ValentinH commented 3 years ago

Sorry for answering so late (I wasn't watching this repo 🙈). I'm not sure to understand what the issue is. Could you share an example?

notthatnathan commented 3 years ago

In my example above, the React error "Can't perform a React state update on an unmounted component" does not trigger a test failure, even though the log confirms it's hitting silenceMessage and returning false.

ValentinH commented 3 years ago

This is weird. Do you have the same behaviour if you remove your custom silenceMessage function?

notthatnathan commented 3 years ago

I'm sorry, I'd moved on to a custom solution, so I don't have that in front of me. My recollection is the tests still passed with those React "warnings" that are actually console.error logs. Don't hold me to it.

ValentinH commented 2 years ago

Ok, I'm closing this then. Please comment if you are able to reproduce the issue so we can re-open it. 🙂