mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Turn Off Console Error Messages In A Test as a javascript til
This commit is contained in:
25
javascript/turn-off-console-error-messages-in-a-test.md
Normal file
25
javascript/turn-off-console-error-messages-in-a-test.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Turn Off Console Error Messages In A Test
|
||||
|
||||
I'm using [Jest](https://facebook.github.io/jest/) to test a React component
|
||||
that requires a prop via
|
||||
[PropTypes](https://reactjs.org/docs/typechecking-with-proptypes.html). 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.
|
||||
|
||||
```javascript
|
||||
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.
|
||||
Reference in New Issue
Block a user