File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 677 TILs and counting..._
13
+ _ 678 TILs and counting..._
14
14
15
15
---
16
16
@@ -268,6 +268,7 @@ _677 TILs and counting..._
268
268
- [ Timing Processes] ( javascript/timing-processes.md )
269
269
- [ Transforming ES6 and JSX With Babel 6] ( javascript/transforming-es6-and-jsx-with-babel-6.md )
270
270
- [ 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 )
271
272
- [ Waiting On Multiple Promises] ( javascript/waiting-on-multiple-promises.md )
272
273
- [ Who Am I: NPM Edition] ( javascript/who-am-i-npm-edition.md )
273
274
- [ Yarn Commands Without The Emojis] ( javascript/yarn-commands-without-the-emojis.md )
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments