Test HOC’s wrapped component with Jest and Enzyme

Probably you’re reading this brief article because you have some React tests failing when trying to access component state and also have a HOC in the project that is applied to the testing component. I had been struggling with this for a couple of hours.

Error

After I ran my tests from the terminal, suddenly a couple of them failed with the following error message: TypeError: Cannot read property ‘something’ of null.

At first I was a bit confused about what could have caused this error.

Solution

I had been looking for the solution and found some good-looking ones, but neither of them worked properly though — some of them just threw more errors than before. After investigating a couple of sites, I found the relevant one which was mentioned in this GitHub issue.

The actual problem is that you can’t access the component within a HOC directly, instead you have to get the appropriate component from a level deeper. This is what you should understand and keep in mind.

If you have any questions, feel free to ask them below!