facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 358 forks source link

15.8.0: `checkPropTypes` sometimes throws: Cannot read properties of undefined (reading 'hasOwnProperty') #369

Closed kaiyoma closed 2 years ago

kaiyoma commented 2 years ago

I tried upgrading from 15.7.2 to 15.8.0 in my project and now I'm getting this error on some of our unit tests:

Warning: Failed prop type: Cannot read properties of undefined (reading 'hasOwnProperty')

I've double-checked that I'm not passing undefined or null to checkPropTypes; all the data is valid and intact. This seems like an error inside checkPropTypes itself.

ljharb commented 2 years ago

aha - I do see one incorrect usage of .hasOwnProperty in factoryWithTypeCheckers, added in #198 and included in v15.8.0. I'll get a fix out shortly.

ljharb commented 2 years ago

Any chance you could provide the propType code you're using that generates that, so I can write a test case?

kaiyoma commented 2 years ago

From what I can tell, our unit test code is passing PropTypes.exact(...) as the first argument to checkPropTypes and then passing an object that should validate.

ljharb commented 2 years ago

Thanks! I'll see if i can repro it.

ljharb commented 2 years ago

hmm, that doesn't seem to do it. Can you perhaps provide the exact code you're using?

kaiyoma commented 2 years ago

Unfortunately this particular unit test code is kind of convoluted (and proprietary), so extracting a workable example isn't that easy. We're passing a multi-level object to PropTypes.exact and a multi-level object as the data to test. It's very difficult to tell which property at which level is causing the problem.

ljharb commented 2 years ago

I was able to reproduce it with a custom propType, but it'd be ideal to use non-contrived code if you can provide it :-)

kaiyoma commented 2 years ago

It really won't be easy for me to provide it (and would require an investment of time I don't have), so I would suggest using your custom prop-type in the interest of getting out a quick fix.

ljharb commented 2 years ago

Filed #370; that should do it. I'll have it released shortly.

ljharb commented 2 years ago

v15.8.1 is released.

kaiyoma commented 2 years ago

Thanks for the quick fix! I just tried it out and my unit tests are passing again. 🎉