prop-types: Add ability to throw error instead of warning in tests
In tests it would be nice to throw an error if the property isn’t of its expected type.
It would be nice if this could be scoped to our own components and not 3rd party ones using 3rd party components, but thats not necesary.
People are already turning console.warning calls to throw errors, so this would be a nice upgrade for them.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 60
- Comments: 26
For anyone else trying to captures prop-type errors in unit tests with Jest - this
setupTest.jsworks for us:This breaks when any test calls
jest.restoreAllMocks()- for us, callingjest.clearAllMocks()` instead helped.It also requires your app to not call
console.errororconsole.warnfor “error handling” (scare quotes, since that’s usually not a good idea).I’m trying to use
prop-typesto validate js schema of external service response on server.And surprised it’s just generating console warnings.
How do I supposed to react to validation errors?
checkPropTypesshould definitely generate some report (like suggested in #88), or at least return true/false. Anyway it would be nice for user to decide how validation results should be handled (log, throw error, etc).This would be really useful for anyone using create-react-app, as PropType errors would appear in the new error overlay
My solution with Jest:
I am not sure what is the current state of this issue and the project.
I am using a small wrapper to throw error like so:
I just published a
check-prop-typespackage that does this. The main export returns the error instead of logging it, and a helperassertPropTypesthrows it.#54 on this repo also has an interesting approach, using a callback instead.
I’m currently using Jest and my solution was to mock the console, then every warning or error will throw an error when running the tests. Hope this can be useful for someone else.
We recently upgraded Jest to latest and had to drop this. I don’t know of any alternatives 😕