Testing Context Providers

Contexts, their providers, and the hooks that use useContext are important parts of many React applications.

Testing them can feel awkward at first, but once you recognise a few common patterns it becomes straightforward.

You will normally need to test things like:

  • are context provider values initialised correctly?
  • do your functions to get context values (wrappers around useContext(SomeContext)) work as expected?
  • can context consumers access the values correctly?

Here is how to easily test them with React Testing Library!

Loading full lesson content & code...

Ready to try running tests for this lesson?