mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Turn Off Console Error Messages In A Test as a javascript til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_677 TILs and counting..._
|
||||
_678 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -268,6 +268,7 @@ _677 TILs and counting..._
|
||||
- [Timing Processes](javascript/timing-processes.md)
|
||||
- [Transforming ES6 and JSX With Babel 6](javascript/transforming-es6-and-jsx-with-babel-6.md)
|
||||
- [Truthiness of Integer Arrays](javascript/truthiness-of-integer-arrays.md)
|
||||
- [Turn Off Console Error Messages In A Test](javascript/turn-off-console-error-messages-in-a-test.md)
|
||||
- [Waiting On Multiple Promises](javascript/waiting-on-multiple-promises.md)
|
||||
- [Who Am I: NPM Edition](javascript/who-am-i-npm-edition.md)
|
||||
- [Yarn Commands Without The Emojis](javascript/yarn-commands-without-the-emojis.md)
|
||||
|
||||
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