checkPropTypes
Manually check PropTypes-compatible proptypes, returning any errors instead of logging them to console.error
.
This function is more suitable for checking propTypes in unit tests than mocking console.error
, avoiding some serious problems with that approach.
Install
$ npm install --save-dev check-prop-types
Usage
Call it just like PropTypes.checkPropTypes
, but it returns any problems as an error message string.
;; const HelloComponent = <h1>Hi name</h1>; HelloComponentpropTypes = name: PropTypesstringisRequired; let result = ;; result = ;;
assertPropTypes
To throw errors instead of returning them, a helper called assertPropTypes
is included:
;; const HelloComponent = <h1>Hi name</h1>; HelloComponentpropTypes = name: PropTypesstringisRequired; ;// fine... ;// throws Error: Failed prop type: Invalid prop `name` of type `number` supplied to `HelloComponent`, expected `string`.
See test.js for more usage examples.