Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 856 Bytes

turn-off-console-error-messages-in-a-test.md

File metadata and controls

25 lines (19 loc) · 856 Bytes

Turn Off Console Error Messages In A Test

I'm using Jest to test a React component that requires a prop via PropTypes. In one of my tests, I want to test that component when the required prop is excluded. The side effect of doing this is that my test output gets cluttered with the PropType warning.

The thing to do is silence the error message during that test.

it('renders a component without a required prop', () => {
  const originalError = console.error;
  console.error = jest.fn();

  // test code here
  expect(shallow(<My Component />)).toDoSomething;

  console.error = originalError;
});

We can silence console.error by temporarily replacing it with a Jest-mocked function and then putting it back at the end of the test.