enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

Add way to suppress error boundary logs #2289

Open spiffytech opened 4 years ago

spiffytech commented 4 years ago

Is your feature request related to a problem? Please describe. When I'm testing a component that throws an error, I get a React error boundary message in my console. This makes it difficult to see what really went wrong with my tests.

function ComponentThatThrowsErrors() {
  if (condition) throw new Error('foo');
  return <p>Pretend I actually did work</p>;
}

----

test('it throws an error', () => {
  expect(mount(<ComponentThatThrowsErrors />)).toThrow();
});

---

$ jest

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Uncaught [Error: foo]

Describe the solution you'd like I'd like an API call I can make that will prevent React from putting the errors into my console

ljharb commented 4 years ago

spyOn(console, 'error')?

spiffytech commented 4 years ago

That would suppress e.g., errors in my tests, or code errors I didn't catch in my tests. I only want to suppress errors that are caught as part of expect().toThrow().

spiffytech commented 4 years ago

From this post, it sounds like there's supposed to be an official React way to suppress the errors, but I'm not sure I whether can do that with the existing Enzyme API.

ljharb commented 4 years ago

That post only describes a browser mechanism to handle it, not a universal one :-/ if you can provide a PR with a test case (mocking out console.error to assert on what's called), i can try to look into it.