Skip to content

Commit 091b4d3

Browse files
committed
Add Turn Off Console Error Messages In A Test as a javascript til
1 parent aa6d7e3 commit 091b4d3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_677 TILs and counting..._
13+
_678 TILs and counting..._
1414

1515
---
1616

@@ -268,6 +268,7 @@ _677 TILs and counting..._
268268
- [Timing Processes](javascript/timing-processes.md)
269269
- [Transforming ES6 and JSX With Babel 6](javascript/transforming-es6-and-jsx-with-babel-6.md)
270270
- [Truthiness of Integer Arrays](javascript/truthiness-of-integer-arrays.md)
271+
- [Turn Off Console Error Messages In A Test](javascript/turn-off-console-error-messages-in-a-test.md)
271272
- [Waiting On Multiple Promises](javascript/waiting-on-multiple-promises.md)
272273
- [Who Am I: NPM Edition](javascript/who-am-i-npm-edition.md)
273274
- [Yarn Commands Without The Emojis](javascript/yarn-commands-without-the-emojis.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Turn Off Console Error Messages In A Test
2+
3+
I'm using [Jest](https://facebook.github.io/jest/) to test a React component
4+
that requires a prop via
5+
[PropTypes](https://reactjs.org/docs/typechecking-with-proptypes.html). In
6+
one of my tests, I want to test that component when the required prop is
7+
excluded. The side effect of doing this is that my test output gets
8+
cluttered with the PropType warning.
9+
10+
The thing to do is silence the error message during that test.
11+
12+
```javascript
13+
it('renders a component without a required prop', () => {
14+
const originalError = console.error;
15+
console.error = jest.fn();
16+
17+
// test code here
18+
expect(shallow(<My Component />)).toDoSomething;
19+
20+
console.error = originalError;
21+
});
22+
```
23+
24+
We can silence `console.error` by temporarily replacing it with a
25+
Jest-mocked function and then putting it back at the end of the test.

0 commit comments

Comments
 (0)